Results 1 to 4 of 4
Thread: JButtonActionPerformed problem
- 06-22-2012, 06:33 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
JButtonActionPerformed problem
Hi everyone,
I've got a JPanel with menu bar and menu items in it. In the design view, I double clicked on one of the items and it led me to code of that item (ActionPerformed). I edited it as I wanted, everything ok.
But now, I need more options. But when I double click the other options it leads me to the code of action performed, but that code is placed inside the action performed of the other one, causing errors all over.
PS: I already have two menu items working, I achieved it by doing the double click on them, but now I need other options and this problem pops when I try to do the same.
Why does this happen? How can it be solved? Can someone help please?
Thanks
Chiapa
- 06-23-2012, 07:42 AM #2
Re: JButtonActionPerformed problem
Cross posted
NetBeans Forums - JButtonActionPerformed problem
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-23-2012, 10:23 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
- 06-23-2012, 05:38 PM #4
Member
- Join Date
- Jun 2012
- Location
- ON, Canada.
- Posts
- 25
- Rep Power
- 0
Re: JButtonActionPerformed problem
C'mon Darryl.. instead of replying just to point that out, you could put forward an answer...
To your question bro... since you're using swing, why not just use an option dialog? and make the buttons an array object like so:
Notice when a button is clicked, it returns an int (the array index of the button). Makes it that much easier to determine the users choice. I think this route is a much simpler alternative to the whole action listeners thing. But I do encourage you to post a chunk of the code you're having trouble with and we can work things out with that. But yeah.. that's it.Java Code:String[] options = {"Button 1", "Button 2", "Button 3"}; int userChoice = JOptionPane.showOptionDialog(null, "Would you like button 1, 2, or 3?", //Dialog message. "Make your Choice", //Title of the dialog. JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, //Dialog type. null, //Icon. options, //The titles of the buttons. options[0]); //Default choice. //Now all you would need to do is make if statements... if (userChoice == 0) { JOptionPane.showMessageDialog(null, "Button 1 to do code.. Blah."); } else if (userChoice == 1) { JOptionPane.showMessageDialog(null, "Button 2 to do code.. Blah Blah."); } else { JOptionPane.showMessageDialog(null, "Button 3 to do code.. Blah Blah Blah."); }
- CHEERS!Last edited by Tha Boss; 06-23-2012 at 05:40 PM.
E.Hughes☆™"I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks