Run function on JFrame close
Asked Answered
K

4

18
void terminate() {}
protected JFrame frame = new JFrame();

How can I get frame to run the terminate function when I press the close button?

Edit: I tried to run this, but for some reason it doesn't print test (however, the program closes). Does anyone have an idea what could be the problem?

frame.addWindowListener(new WindowAdapter() {
    public void WindowClosing(WindowEvent e) {
        System.out.println("test");
        frame.dispose();
    }
});
Kirbykirch answered 4/5, 2013 at 8:28 Comment(0)
H
26

You can use addWindowListener:

frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        // call terminate
    }
});

See void windowClosing(WindowEvent e) and Class WindowAdapter too.

Heid answered 4/5, 2013 at 8:29 Comment(2)
Will I have to create a new class that extends JFrame and implements WindowAdapter? (or is it ok to keep frame as a JFrame?) It doesn't really work yet, and I get the warning "The method WindowClosed(WindowEvent) from the type new WindowAdapter(){} is never used locally" on the WindowClosing functionKirbykirch
windowClosing and not WindowClosing with uppercase W.Heid
B
20

Not only do you have to add the window listener, you have to set the default close operation to do nothing on close. This allows your code to execute.

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent event) {
        exitProcedure();
    }
});

Finally, you have to call System exit to actually stop your program from running.

public void exitProcedure() {
    frame.dispose();
    System.exit(0);
}
Beacon answered 4/5, 2013 at 9:41 Comment(2)
+1 for the answer, but especially @Override. Very good advice. No (reconsiders) excellent advice. :)Leadsman
Remove the frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);Bailiff
Q
2

Frame.dispose() method does not terminate the program. To terminate the program you need to call System.exit(0) method

Quatrefoil answered 18/11, 2014 at 16:15 Comment(0)
L
1

If you want to terminate your program after the JFrame is closed, you have to set the default close operation on your JFrame.

In your constructor of your JFrame write:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

If you just want to call a method when the window is closed and not terminate the whole program, than go with the answer of Maroun.

Labbe answered 4/5, 2013 at 9:7 Comment(2)
I would like to do both (run a function, then terminate). Here's my current code: frame.addWindowListener(new WindowAdapter() { public void WindowClosing(WindowEvent e) { System.out.println("test"); frame.dispose(); } }); The problem is that it doesn't print "test". Do you have any idea what could be the problem?Kirbykirch
The method is "windowClosing" not "WindowClosing". You need to override it, so it has to be the exact name.Labbe

© 2022 - 2024 — McMap. All rights reserved.