Question

I'm using a custom JFrame to implement a simple dialog in a Java application I'm working on.

After the user pushes the 'Apply' button in the window, it should close.

What would be the most conventional way to do this? Is setVisible(false) inside the class the best way? Is there any way more recommended?

Was it helpful?

Solution

To close a window in Swing like JFrame or JDialog you have two options.

Call dispose()

Just call dispose() method:

public void dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

For instance:

frame.dispose();
dialog.dispose();

Dispatch a WINDOW_CLOSING event

You can dispatch a new WindowEvent like this:

frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));

If the window has attached a WindowListener it will be notified. If the frame's (or dialog's) default close operation is set then this action will be performed. The possible close operations are:

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
  • EXIT_ON_CLOSE (defined in JFrame and not available for JDialog): Exit the application using the System exit method. Use this only in applications.

OTHER TIPS

It depends on what you're trying to achieve. Setting the window to not visible will simply hide it but it will still be running in the background (JFrame/InternalFrame). You can use JDialog (See JOptionPane as an example) to create temporary frames which are truly closed when clicking on one of the buttons. You can also retrieve the selected option when the user closed the window (here : Javadoc). You can also forcibly close a window by firing an event to close it, like so:

Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
                    new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)
             )

Inside an actionlistener for example.

There are multiple operations which can be performed when you close a JFrame.

Suppose you have a JFrame

JFrame frame = new JFrame();

This one exits the JVM when closed.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This one just hides the JFrame.

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

This one disposes the JFrame.

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate

And the default is do nothing.

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top