Wednesday, September 20, 2006

How to Close a Swing JFrame Without Exiting?

I have a main JFrame and a sub JFrame that opens based on a double click event on a JTable. This second JFrame has details about the record double clicked. When trying to close the second JFrame the entire application closes. I just want the sub JFrame to close and return you to the original JFrame. Let's see what google says about this...

Google Search

From this I found a simple definition of the close function. Here it is.

I want to close my JFrame either by the user clicking on the "X" or when the user clicks on the "Save" button.

The simplest way to do this is to add one of the four to your JFrame code:
this.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
this.setDefaultCloseOperation ( JFrame.HIDE_ON_CLOSE );
this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
this.setDefaultCloseOperation ( JFrame.DO_NOTHING_ON_CLOSE )
;