简体   繁体   中英

Starting an event after closing JFrame window

After i close my window,i want to create an object of a class and preform a task. After closing:

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

What can i do so that it can start a new operation as soon as window is closed? please help.

When the user tries to close a JFrame , the following events happen :

  • the windowClosing method of the WindowListener s registered on that frame are invoked.
  • then, the action specified by setDefaultCloseOperation is triggered.

There are 4 default close operations :

do nohting on close

...does nothing at all. The frame is still here, happily ignoring your requests to close it.

hide on close

Same effect as calling setVisible(false) on the frame. The frame becomes invisible, and it can be displayed again by calling setVisible(true) .

dispose on close

Same effect as calling dispose() on the frame. The effect is somewhat similar to HIDE_ON_CLOSE, the difference is that this time, the OS resources used by the frame are released. You can still call setVisible(true) again if you want to make the frame appear again.

The windowClosed method of the WindowListener s registered on that frame are invoked.

Also, note that when the last displayable window is disposed of, your program may terminate .

exit on close

Same effect as calling System.exit() .


So, if you want to have full control on what happens when the user tries to close a frame, set the default operation to DO_NOTHING_ON_CLOSE, and put your code in the windowClosing method of a WindowListener . There, you can call setVisible(false), dispose() or System.exit(), depending on what you want to achieve.

You can also set the default operation to hide, dispose, or exit on close, and put your code in the windowClosing method of a WindowListener . In this case, the listener will be invoked first, and then the chosen action will be executed, no matter what.

Finally, you can also set the default operation to DISPOSE_ON_CLOSE, and put your code in the windowClosed method of a WindowListener . That way, the frame is first disposed (it disappears from the screen), and then your code gets executed.

Call

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

and register a listener for the closing event:

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        JFrame frame = (JFrame)e.getSource();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // additional code here
    }
});
After i close my window,i want to create an object of a class and preform a task.

look at

  • HIDE_ON_CLOSE simplair as setVisible(false); , container is re_usable, is possible to show it by calling setVisible(true);

  • or you can to call dispose() , meaning that this container isn't re_usable, have to check isDisplayable() , before calling setVisible(true);

i want to create an object of a class and preform a task

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM