Java (Swing) question. Closing a frame.

Status
Not open for further replies.

Plasdom

Ars Tribunus Angusticlavius
6,704
I have a program that brings up a frame when a user selects an option from the menu. I used a contentpane to add the components onto the frame. These components consist of textfields for input and two buttons: Enter and cancel; and implements actionlisteners.

"Enter" accepts the input from the textfields and then the frame is supposed to close. With cancel, the frame just closes.

Now the problem is, to "close" the frame I just use
Code:
 setVisible(false);
This works alright but I was wondering if that was the right way to do it. Will it be better to write a close() function for the class? If so how do I close the frame.

Here are snippets from the class that creates and brings up the frame.
Code:
 public class Foo extends JFrame implements ActionListener
{ 
....
public void actionPerformed (ActionEvent event)
{
....
if (bClicked.equals("Enter")) {
//do something here
}
else
if (bClicked.equals("Cancel")) {
setVisible(false);}
 
Status
Not open for further replies.