Buttons problem

Reply

Join Date: Oct 2005
Posts: 3
Reputation: jeni_4 is an unknown quantity at this point 
Solved Threads: 0
jeni_4 jeni_4 is offline Offline
Newbie Poster

Buttons problem

 
0
  #1
Oct 22nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Buttons problem

 
0
  #2
Oct 22nd, 2005
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:

  1. public void actionPerformed(ActionEvent ae)
  2. {
  3. if (ae.getSource() == doneButton)
  4. {
  5. System.exit(0);
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 46
Reputation: Gargol is an unknown quantity at this point 
Solved Threads: 1
Gargol's Avatar
Gargol Gargol is offline Offline
Light Poster

Re: Buttons problem

 
0
  #3
Oct 24th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Buttons problem

 
0
  #4
Oct 24th, 2005
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 3
Reputation: jeni_4 is an unknown quantity at this point 
Solved Threads: 0
jeni_4 jeni_4 is offline Offline
Newbie Poster

Re: Buttons problem

 
0
  #5
Oct 24th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: Buttons problem

 
0
  #6
Oct 25th, 2005
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...

  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...
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC