Results 1 to 4 of 4
- 04-03-2011, 08:09 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Multiple JButton/ActionListener problem
I am unable to implement more than one actionlistener. I have two questions, 1, how should I code this, and 2) is my class / program design off? would like not only how to make it work but any advice on doing it (program/class design) correctly.
I have one actionlistener and several buttons. I want to use if statements in the action performed method to decide which action to do. When I run the program, it says listener is not iniatialized. I have found examples on the Internet to initialize the listener, but I couldn't figure out how to use that method for several buttons.
Here's my code:
Java Code:public class FlashCard { public FlashCard() { OperationsWindow(); DisplayQuestionWindow(); } public void OperationsWindow() { //TODO OperationsWindow covers entire page //asks if Question, Add Question, or Exit int w; int h; ActionListener listener; Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); JFrame opsF = new JFrame("FlashCard"); Container content =opsF.getContentPane(); content.setLayout(new FlowLayout()); //format to add Button //declare jbutton, initialize name //create a listener for that button //add listener to button //enable button //add button to jframe JButton jbQuit = new JButton("Quit"); jbQuit.addActionListener(listener); jbQuit.setEnabled(true); opsF.add(jbQuit); JButton jbStart = new JButton("Start"); jbStart.addActionListener(listener); jbStart.setEnabled(true); opsF.add(jbStart); w=(int) dim.getWidth(); h=(int) dim.getHeight(); opsF.setSize(w-4, h-400); opsF.setDefaultCloseOperation(opsF.EXIT_ON_CLOSE); //opsF.pack(); opsF.setVisible(true); } public void actionPerformed(ActionEvent a) { String str = a.getActionCommand(); System.out.println("str is: "); if(a.getSource() == "Quit") { System.out.println("Start button was pressed..."); } //if(a.getSource() == stopButton){ // System.out.println("Stop button was pressed..."); }Last edited by Fubarable; 04-03-2011 at 08:14 PM.
-
Where's the rest of your class? The imports? The main method?
I've added code tags to your post above so that your code will retain its formatting and we can better be able to read it. To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Good luck!
-
Also look into using anonymous inner classes as one way to create your ActionListeners. For instance: best-way-implement-actionlistener
- 04-04-2011, 12:48 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Problem with ActionListener?
By Manish87 in forum AWT / SwingReplies: 5Last Post: 03-31-2011, 02:54 PM -
JButton ActionListener problems
By Fortu in forum New To JavaReplies: 6Last Post: 02-05-2011, 04:20 PM -
JButton actionlistener not working, very strange
By alacn in forum New To JavaReplies: 4Last Post: 08-05-2010, 02:25 AM -
one inner class(an interface (ActionListener))) for multiple actions
By bigj in forum New To JavaReplies: 6Last Post: 02-01-2010, 09:30 AM -
[SOLVED] JButton actionlistener, if statement, JLabel visibility
By JonoF in forum New To JavaReplies: 2Last Post: 04-19-2009, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks