944,018 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1628
  • Java RSS
Oct 22nd, 2005
0

Buttons problem

Expand Post »
I have a JFrame , and to one of its button 'done', I wish to add an ability to close the window...ie on clicking done this window should close.

jen
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeni_4 is offline Offline
3 posts
since Oct 2005
Oct 22nd, 2005
0

Re: Buttons problem

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:

Java Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent ae)
  2. {
  3. if (ae.getSource() == doneButton)
  4. {
  5. System.exit(0);
  6. }
  7. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 24th, 2005
0

Re: Buttons problem

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
Gargol is offline Offline
46 posts
since Oct 2005
Oct 24th, 2005
0

Re: Buttons problem

Quote 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.
Not really. There are two ways of doing this:
Java Syntax (Toggle Plain Text)
  1. JButton doneButton = new JButton("Done");
  2.  
  3.  
  4. if (ae.getSource() == doneButton)
  5. {
  6. }
  7.  
  8.  
  9. or
  10.  
  11. if (ae.getActionCommand().equals("Done"))
  12. {
  13. }

You can compare the actual object itself, or the name of it.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Oct 24th, 2005
0

Re: Buttons problem

Well!! Thanks every one for their reply.

I has actually tried system.exit(0), but that closes the complete application. However looking through the API I realised that I could use dispose()..i tried that and it works fine.

Thanks every one once again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeni_4 is offline Offline
3 posts
since Oct 2005
Oct 25th, 2005
0

Re: Buttons problem

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...

Java Syntax (Toggle Plain Text)
  1. public class ExtendedJButton extends JButton implements ActionListener
  2. {
  3. public ExtendedJButton()
  4. {
  5. init();
  6. }
  7.  
  8. private void init()
  9. {
  10. addActionListener(this);
  11. }
  12.  
  13. public void actionPerformed(ActionEvent event)
  14. {
  15. System.out.println("Override this method with an anonymous inner clas");
  16. }
  17. }

Then to use this class do the following when creating the button...
Java Syntax (Toggle Plain Text)
  1. JButton jButton = new ExtendedJButton()
  2. {
  3. public void actionPerformed(ActionEvent event)
  4. {
  5. System.out.println("I'm suppose to do something exciting here...");
  6. }
  7. };

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
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help me!!!
Next Thread in Java Forum Timeline: multimedia in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC