954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Buttons problem

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

jeni_4
Newbie Poster
3 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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:

public void actionPerformed(ActionEvent ae)
{
       if (ae.getSource() == doneButton)
       {
               System.exit(0);
       }
}
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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.

Gargol
Light Poster
46 posts since Oct 2005
Reputation Points: 10
Solved Threads: 1
 

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:

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

jeni_4
Newbie Poster
3 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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

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

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

hooknc
Posting Whiz in Training
219 posts since Aug 2005
Reputation Points: 11
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You