Jframe close all windows

How to close multiple JFrame and JDialog windows?

I’m working on a program which has multiple JFrame and JDialog windows.

I have a JFrame which contains a button, when I click on this button a JDialog window opens up. In this JDialog windows there is another button, which when is clicked it opens up a second JDialog window. In the second JDialog window I have a last button.

What I want to do is to close both JDialog windows and JFrame window when this last button is clicked.

This is how the opening order is:

I have tried super.dispose(); but it doesn’t work. Any ideas?

3 Answers 3

As shown here using Action , your actionPerformed() implementation can dispatch the WINDOW_CLOSING event to the desired Window instances.

There may be better ways of doing this, but here is one general approach that might help.

In your code you create the windows but you do not store the reference to the windows you created into a variable. For example, you have:

Then later, when you create the instance of Dialog1, you have this code:

This means you have created the Dialog, but you have not retained a reference to the Dialog for later manipulation by your code. If you assign this value here, you should be able to manipulate it later.

If you change your implementation to:

Then later in your code you will have a reference to the Dialog in order to manipulate it,

How to prevent JFrame from closing

I have a Java GUI application from which another java GUI application is invoked using reflection and loading. It works fine the only problem faced is, on closing the JFrame of invoked application the Main GUI application frame also closes. How can I prevent the main application (frame) from closing??

I cannot change the defaultCloseOperation of the invoked application, However a change to the main application can be made. Does it have any thing to do with threads??

This is my applications code that executes a target application

3 Answers 3

I am not allowed to make changes to the application being invoked.

That was a comment in reply to @JeffLaJoie just to clarify, it would not require any changes to the code of the other app., just an extra method call or two by your app. at run-time to set the close operation of the 3rd party frame.

Failing that, the best solution I can think of is to start the new frame in a separate Process that starts a new JVM, when the user closes the other app., it and the 2nd JVM will end, while leaving the original app. on-screen.

Читайте также:  Как убрать задачу с панели задач windows

You’ll want to use the DISPOSE_ON_CLOSE operation, so it would be setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)

EXIT_ON_CLOSE would be the option that closes all windows which I believe is what you are currently experiencing.

You have the following options for the defaultCloseOperation :

  • DO_NOTHING_ON_CLOSE — The do-nothing default window close operation;
  • HIDE_ON_CLOSE — The hide-window default window close operation;
  • DISPOSE_ON_CLOSE — The dispose-window default window close operation.
  • EXIT_ON_CLOSE — The exit application default window close operation. Attempting to set this on Windows that support this, such as JFrame, may throw a SecurityException based on the SecurityManager. It is recommended you only use this in an application.

The Option DISPOSE_ON_CLOSE could be used in order to avoid to close all windows, closing just the one you want.

If you don’t have direct access to JFrame object as you have with the last posted code, you could use Window.getWindows() in order to receive all windows instance (as JFrame is a Window too it will be listed too). And then set the defaultCloseOperation on that.

Possibly you will need to use threads because the defaultCloseOperation needs to be set after invoke main method.

Theoretically it works, so I think this is a good shot 😉

How to programmatically close a JFrame

What’s the correct way to get a JFrame to close, the same as if the user had hit the X close button, or pressed Alt + F4 (on Windows)?

I have my default close operation set the way I want, via:

It does exactly what I want with the aforementioned controls. This question isn’t about that.

What I really want to do is cause the GUI to behave in the same way as a press of X close button would cause it to behave.

Suppose I were to extend WindowAdaptor and then add an instance of my adaptor as a listener via addWindowListener() . I would like to see the same sequence of calls through windowDeactivated() , windowClosing() , and windowClosed() as would occur with the X close button. Not so much tearing up the window as telling it to tear itself up, so to speak.

17 Answers 17

If you want the GUI to behave as if you clicked the X close button then you need to dispatch a window closing event to the Window . The ExitAction from Closing An Application allows you to add this functionality to a menu item or any component that uses Action s easily.

If by Alt-F4 or X you mean «Exit the Application Immediately Without Regard for What Other Windows or Threads are Running», then System.exit(. ) will do exactly what you want in a very abrupt, brute-force, and possibly problematic fashion.

If by Alt-F4 or X you mean hide the window, then frame.setVisible(false) is how you «close» the window. The window will continue to consume resources/memory but can be made visible again very quickly.

Читайте также:  Часы несколько часовых поясов windows

If by Alt-F4 or X you mean hide the window and dispose of any resources it is consuming, then frame.dispose() is how you «close» the window. If the frame was the last visible window and there are no other non-daemon threads running, the program will exit. If you show the window again, it will have to reinitialize all of the native resources again (graphics buffer, window handles, etc).

dispose() might be closest to the behavior that you really want. If your app has multiple windows open, do you want Alt-F4 or X to quit the app or just close the active window?

The Java Swing Tutorial on Window Listeners may help clarify things for you.

Jframe close all windows

The JFrame class is slightly incompatible with Frame . Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame . This is different from the AWT Frame case. As a convenience, the add , remove , and setLayout methods of this class are overridden, so that they delegate calls to the corresponding methods of the ContentPane . For example, you can add a child component to a frame as follows: And the child will be added to the contentPane. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it. Refer to RootPaneContainer for details on adding, removing and setting the LayoutManager of a JFrame .

Unlike a Frame , a JFrame has some notion of how to respond when the user attempts to close the window. The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int) . To make the JFrame behave the same as a Frame instance, use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) .

For more information on content panes and other features that root panes provide, see Using Top-Level Containers in The Java Tutorial.

In a multi-screen environment, you can create a JFrame on a different screen device. See Frame for more information.

Warning: Swing is not thread safe. For more information see Swing’s Threading Policy.

Setting the Behavior When Closing a JFrame

There are a number of techniques that one can use to close a frame in Java. Each has its advantages and disadvantages. Three techniques are presented below in general decreasing preference order:

JFrame.defaultCloseOperation(int option)

This is the simplest technique. All you need to do is to call the defaultClostOperation method of a JFrame , supplying the desired option value.

Читайте также:  Спящий режим usb windows

The possibilities for the option value are:

  • JFrame.EXIT_ON_CLOSE — A System.exit(0) call will be executed, exiting the entire application.
    • Do NOT use this option if the program is running as an applet as this will cause the browser to crash!
  • JFrame.DISPOSE_ON_CLOSE — The frame will be closed and disposed but the application will not exit.
  • JFrame.DO_NOTHING_ON_CLOSE — The frame will be closed but not disposed and the application will not exit.

The advantage of the technique is that it is very simple and easy, but it does not allow much flexibility to do any custom operations during the frame closing.

For dynamically configurable frame closing behavior, supply the option value to the constructor of the frame. This would be done by the controller, which would instantiate the frame with either an EXIT_ON_CLOSE option if it was an application or a DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE if it were an applet.

java.awt.Window.addWindowListener(java.awt.event.WindowListener l)

(Note that javax.swing.JFrame is a subclass of java.awt.Window and thus inherits this method.)

In this technique a WindowListener interface implentation is added to the frame, where the listener has a method, windowClosing() , that is called when the frame is closed.

In practice, on overrides the windowClosing() method of WindowAdapter , a no-op implementation of WindowListener . This way, one doesn’t have to worry about all the other methods of the WindowListener .

This technique has the advantage that any sort of custom processing can be done when the frame is closed. The frame can be constructed with a command that the windowClosing method calls or with an complete WindowListener object, thus enabling dynamic configuration of the frame closing behavior. This is very useful when you want to run the same frame as either a regular application or as an applet — the controller will instantiate the frame with the proper closing behavior.

The disadvantage of this technique is the larger code required than the first technique for standard window closing behaviors.

java.awt.Window.processWindowEvent(java.awt.event.WindowEvent e)

(Note that javax.swing.JFrame is a subclass of java.awt.Window and thus inherits this method.)

In this technique, the processWindowEvent method, which is called when the frame is closed, is overriden to provide the desired behavior. The supplied WindowEvent parameter is tested to see if it is the event for the window is being closed and if so, the desired behavior is executed.

For dynamic behavior, a command supplied to the constructor of the frame can be run when the WINDOW_CLOSING event is detected.

The advantage of this technique is that an entire object to handle the frame closing needs to be constructed but at the expense of a more complicated logical process.

Оцените статью