Close all java windows

How to close all children and parent windows?

I need to close all the children and parent windows when I close one window in the hierarchy.

I have three lines of windows:

My windows hierarchy — All lines start in one MainWindow . Only LoadPlayers and CreatePlayers are dialogs. All other windows are frames.

E.g. I close TablesOverview in the first line — I need to close all other windows in this line.

But windows in other lines must stay open.

Notice that TablesOverview is in two lines.

I can write the code, where I named every window that must close. But I need cleaner solution.

This code give me all opened windows — I don’t know how to take only windows in one line.

This codes give me nothing.

1 Answer 1

Java don’t provides this support implicitly. So, you have to use some work around:

Solution:

  • Create a new class which extends JFrame
  • Create a data member children of type ArrayList . This holds the instance created by current window
  • Create an member method createChildWindow() . This method will create a child window and add it in to variable children
  • Register for event on closing the window. For this use addWindowListener() and override public void windowClosing(java.awt.event.WindowEvent windowEvent) method. In this method traverse throw the children collection and close each child(This will eventually close its subsequent children also).

Following is the complete working code

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.

Читайте также:  Partition для windows server 2012

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.

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.

Close All Windows at Once — Safely & Easily Quit Apps Before Shutting Down Your PC

Close All Windows (or CloseAll) is an ultimate task management tool for Windows designed specifically to quickly close multiple applications. CloseAll flashes a ‘close’ signal to the selected applications and then ceases. It doesn’t use any system resources at all, since you run it only when needed. What can be easier than a task list with check boxes and OK button? Yes, you can run CloseAll without any UI too!

Читайте также:  Windows 10 как сделать боковую панель

Pay once, use forever on any PC you own!

BLACK FRIDAY SALE: 35% off this week only!

CloseAll allows you to choose different sorting and grouping options for the task list, double-click groups to select/deselect the whole group, filter apps by typing in any part of their window title or app name, and use individual close buttons to close apps one by one.

Screenshots created with WinSnap – 20% discount if bought together with CloseAll!

New dark theme is default now:

Start typing to search for apps quickly:

Command Line Usage

You can specify /NOUI command line switch to run CloseAll in silent mode and close all windows without any UI interaction. Just open CloseAll shortcut properties and add /NOUI to the “Target” location:

CloseAll is indeed very handy if you are running

20 applications at the same time and want all of them to quit instantly. Try it now to see if it saves you time!

Pay once, use forever on any PC you own!

BLACK FRIDAY SALE: 35% off this week only!

CloseAll runs only on Windows 10, 8, 7 and Vista (32-bit and 64-bit). The native 64-bit version is included in the setup package and installed automatically.

Never miss when a new version comes out! Subscribe to updates →

close window event in java

I added an window state listener as follow:

But when I’m using the X close button the event is not called. I think it’s something to do with netbean jdesktop framework. But I can’t find what could be the problem. Thanks for your help.

7 Answers 7

windowClosing is part of the WindowListener interface. Use addWindowListener instead of addWindowStateListener .

Normally you use a WindowListener for this.

Check out Closing an Application for an approach I use, although I must admit I’ve never tried it with Netbeans since I don’t use an IDE.

Not answering your question directly (since an answer has already been given), but I assume you want to quit your program (or just hide a window) on exit. There is a shorter solution for these situations:

Just debugged a similar problem in my Swing program. It turned out to be a Java bug that kills the system UI events when ImageIO is called before the Swing UI is created. See here for a minimal example —

Читайте также:  Как смонтировать виртуальный диск windows 10

This bug stops the system UI events, such as window-close, from being delivered to Java.

As the Java documentation says, to actually close the window the listener should invoke the window’s dispose or setVisible(false) method.

Thank you everyone for helping me solve the problem. I don’t fully understend it but the following code solve the problem:

It look as if the framework add additional frames. Thanks,

As others have pointed out the WindowListener is what you are after. but you should do this from now on when overriding a method:

Then the compiler will tell you when you are not actually overriding a method and are instead adding a new method (that in this case will never be called).

java swing close window without exiting app

I have a little frame where I ask user & password. This frame will be opened clicking over a button in a main window.

Then I have two buttons: ok and cancel.

When I click on «cancel» button, I need to close this frame without exiting the app.

How can I do that?

8 Answers 8

You can use either Frame.hide() or Frame.dispose(). I would also recommend to look into JDialog or JOptionPane

Correction: hide() is deprecated. SetVisible(false) should be used instead

Maybe a cleaner way is just change the setDefaultCloseOperation from EXIT_ON_CLOSE to DISPOSE_ON_CLOSE :

You can call setVisible(false) on the frame.

You might also want to call setDefaultCloseOperation on the frame passing in HIDE_ON_CLOSE (info here: http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29). That will prevent the app from going away if they user hits the «X» on the JFrame to close it.

Use this.dispose(); in the action listener method when the username/password succeeds. eg:

If you are using inner classes to handle the events just replace ‘this.dispose()’ with Super_Class_Name.this.dispose();

Make sure you do not:

setVisible method does not release memory resources and should be used only when the form is to be used again.

The dispose method 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.

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