JFrame catch dispose event
Asked Answered
S

3

11

I have a Java project.
I have a JFrame with a handler attached to it like so

frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent evt) {
                this.setEnabled(true);

            }
        });

But, on that frame I also have a close button (to make it more user friendly) and that "close" button calls the frame dispose method. Now, when I close the frame by clicking the little x button on the top right corner, the WindowListener is called. But the event doesn't fire when I invoke the dispose method.
should I Invoke some other method to close, so the WindowListener fires, or maybe implement another listener?

Samantha answered 11/11, 2010 at 13:17 Comment(0)
L
3

on that frame i also have a close button (to make it more user friendly)

Check out the Closing an Application solution to handle this. All you really need to do is add the "ExitAction" to your button, but you can use the whole approach if you want.

Landsman answered 11/11, 2010 at 16:30 Comment(0)
S
12

You should take a look at the WindowListener interface.

windowClosing(): Invoked when the user attempts to close the window from the window's system menu. (window X button)

windowClosed(): Invoked when a window has been closed as the result of calling dispose on the window.

So, windowClosing() is called only when the user clicks the X button of the window; windowClosed() is called when the dispose() event is invoked, so it is always invoked:

  • if the user closes the frame using the windows X button
  • if the frame is programatically closed by code
    JFrame myFrame = new JFrame();
    myFrame.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosed(java.awt.event.WindowEvent windowEvent) {
            // your code
        }
    });

Source: https://alvinalexander.com/blog/post/jfc-swing/closing-your-java-swing-application-when-user-presses-close-but

Smidgen answered 4/1, 2018 at 21:36 Comment(2)
Beware: when using windowClosed, the window is already gone when the handler is called. Keep this in mind when you want to cleanup some resources (OpenGL context ...), as windowClosed may be already too late for this.Uveitis
do you know why that information is not available here: docs.oracle.com/javase/7/docs/api/java/awt/event/…?Jaqitsch
L
3

on that frame i also have a close button (to make it more user friendly)

Check out the Closing an Application solution to handle this. All you really need to do is add the "ExitAction" to your button, but you can use the whole approach if you want.

Landsman answered 11/11, 2010 at 16:30 Comment(0)
U
1

If you want to catch dispose reliably, no matter how it is called, you can override the dispose method. Usually you want to call super.dispose() and implement any custom handling before or after it as suitable for given task.

        JFrame frame = new JFrame("FrameDemo") {
            @Override
            public void dispose() {
                System.out.println("On dispose");
                super.dispose();
            }
        };
Uveitis answered 30/12, 2022 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.