java - Calling a Method That Takes a Parameter Within ActionListener -
i've encountered problem while trying call method within class implements actionlistener. method being called, datacompiler, needs use integer wordcountwhole, returned in wordcount class. problem can't pass required parameter actionlistener method.
import javax.swing.*; import java.awt.*; import java.awt.list; import java.awt.event.*; import java.beans.propertychangelistener; import java.text.breakiterator; import java.util.*; import java.util.stream.intstream; public class gui extends jframe { public jtextarea textinput; public jbutton databutton; public string str; public gui() { super("text miner"); pack(); setlayout(null); databutton = new jbutton("view data"); //button take user data table databutton.setsize(new dimension(120, 50)); databutton.setlocation(5, 5); handler event = new handler(); //adds action listener each button databutton.addactionlistener(event); add(databutton); public class wordcount { public int miner() { //this returns integer called wordcountwhole } } public class handler implements action { //all possible actions when action observed public void action(actionevent event, int wordcountwhole) { if (event.getsource() == graphbutton) { graphs g = new graphs(); g.graphs(); } else if (event.getsource() == databutton) { datacompiler dc = new datacompiler(); dc.data(wordcountwhole); } else if (event.getsource() == enterbutton) { wordcount wc = new wordcount(); sentencecount sc = new sentencecount(); wc.miner(); sc.miner(); } } } } and here's code datacompiler class:
public class datacompiler{ public void data(int wordcountwhole){ int m = wordcountwhole; system.out.println(m); } }
you don't add parameter there because you've invalidated contract of interface.
use constructor* (see note below, first)
public class handler implements action{ //all possible actions when action observed private int wordcountwhole; public handler(int number) { this.wordcountwhole = number; } @override public void actionperformed(actionevent event) { although, isn't entirely clear why need number. datacompiler.data method prints number passed it, , variable seemingly comes in code because not passed actionlistener.
* should instead use integer.parseint(textinput.gettext().trim()) inside handler class / listener code , not use constructor. otherwise, you'd number value when add handler, empty string , throw error because text area has no number in it.
additionally, wc.miner(); returns value, calling on own without assigning number throws away return value.
Comments
Post a Comment