Hello there,

In one of my projects I'm working on currently. I have a 'cancel' button in my GUI window that is suppose to close the JFrame the button resides in. I have done some research on the web about this. But it seems like one has to write a fair amount of code for this little trival task. Does anyone know of a simple/short way of doing this?

Any help would greatly be appreciated,
Thanks!

As far as I know, if the instance of JFrame is a, the code for registering its closing button could be one line of code as follows:

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Well, what is your idea of a "fair amount of code"? Why don't you show us what it is you've tried?

As far as I know, if the instance of JFrame is a, the code for registering its closing button could be one line of code as follows:

a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

No, that line of code describes what should happen when the frame closes. It does not close the frame.

Member Avatar for exonwarrior

I'm also looking for a solution. I tried the

.dipose()

method I found while googling, but to no avail.

I've tried this -->

/* This is from the application piece */
JFrame myframe = new JFrame("My Frame");
myframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Runtime.getRuntime().exit(0);
}
});

/* This is from the action event of the button */
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myframe.dispatchEvent(new WindowEvent(close,WINDOW_CLOSING);
}
});

Seriously, just to close a Frame? It's ridiculous..

If you want to exit the app you don't need lines 3-7, you can just do that directly on line 13
If you don't want to exit, you don't need lines 3-7

So in the end it's 3 lines of boilerplate (excluding {}).

I've tried this -->

/* This is from the application piece */
JFrame myframe = new JFrame("My Frame");
myframe.addWindowListener(new WindowAdapter() {
     public void windowClosing(WindowEvent e) {
        Runtime.getRuntime().exit(0);
     }
  });

Don't use addWindowListener (you can but it's not current and not suggested for "standard" activities). Rather then that use the line suggested earlier.

/* This is from the action event of the button */
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        myframe.dispatchEvent(new WindowEvent(close,WINDOW_CLOSING);
     }
  });

This part is good. You can either continue to use it as is, or change the dispatchEvent line to a combination of setVisible(false) and dispose.

Seriously, just to close a Frame? It's ridiculous..

No it's not, it's generic. Regardless of what action you want to take the action is coded the same way. In fact, by using the WindowListener version above you can then have the program take some specific actions before closing such as flushing streams, closing varying types of connections, etc, etc. It is, however, better to have those actions (if you are going to do it in that manner) is a shutdownHook and simply call exit (as above).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.