| | |
Buttons problem
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
Since your using swing it's easy:
In the action performed method, just check which button was clicked and then add this line of code:
System.exit(0);
for example:
In the action performed method, just check which button was clicked and then add this line of code:
System.exit(0);
for example:
Java Syntax (Toggle Plain Text)
public void actionPerformed(ActionEvent ae) { if (ae.getSource() == doneButton) { System.exit(0); } }
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by Gargol
jeni
Remember that java matches on object references so the reference to doneButton should be the name you gave to the button object when you made it.
Java Syntax (Toggle Plain Text)
JButton doneButton = new JButton("Done"); if (ae.getSource() == doneButton) { } or if (ae.getActionCommand().equals("Done")) { }
You can compare the actual object itself, or the name of it.
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
I personally like making a button a listener of itself instead of having if statements determining what is suppose to happen when a specific button is pressed...
Then to use this class do the following when creating the button...
Some people might say that this is over kill, but i think a button should know what it is suppose to do and what if a panel has over a hundered buttons on it? Do you really want to have a 100 case if statement?
Regards,
Nate
Java Syntax (Toggle Plain Text)
public class ExtendedJButton extends JButton implements ActionListener { public ExtendedJButton() { init(); } private void init() { addActionListener(this); } public void actionPerformed(ActionEvent event) { System.out.println("Override this method with an anonymous inner clas"); } }
Then to use this class do the following when creating the button...
Java Syntax (Toggle Plain Text)
JButton jButton = new ExtendedJButton() { public void actionPerformed(ActionEvent event) { System.out.println("I'm suppose to do something exciting here..."); } };
Some people might say that this is over kill, but i think a button should know what it is suppose to do and what if a panel has over a hundered buttons on it? Do you really want to have a 100 case if statement?
Regards,
Nate
![]() |
Similar Threads
- A Little Help. (Site Layout and Usability)
- How to Print a Memo field ? (C++)
- Memory buttons problem (Java)
Other Threads in the Java Forum
- Previous Thread: help me!!!
- Next Thread: multimedia in java
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows






