Run function on JFrame close

36,943

Solution 1

You can use addWindowListener:

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

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

Solution 2

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);
}

Solution 3

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

Solution 4

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.

Share:
36,943

Related videos on Youtube

Mattias
Author by

Mattias

var webSocket = new WebSocket('ws://127.0.0.1:1234'); var requests = {}; webSocket.onmessage = function(unparsedMessage){ console.log("Received message " + unparsedMessage); var message = JSON.parse(message.data); var options = requests[value.requestId] delete requests[value.requestId]; options.success(value); }; Backbone.sync = function(method, model, options){ requestID = GuidGenerator.generateGuid(); var message = JSON.stringify({ messageType options.messageType, requestID: requestID, data:model.attributes }); webSocket.send(message); console.log("Sending message " + message); requestMap[requestID] = options; }; var test= new Test(); test.fetch({ messageType: 'TEST' }); GuidGenerator { var generateGuid = function generateUUID(){ var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random()*16)%16 | 0; d = Math.floor(d/16); return (c=='x' ? r : (r&0x3|0x8)).toString(16); }); return uuid; }; } type BlahId= {blahId: number}; type BlahCode= {blahCode: string}; function simpleFunction(data: blahId) { console.log(data.blahId); } type Blah = { blahId: BlahId, blahCode: BlahCode } function advancedFunction(blah: Blah) { console.log(blah.blahId); } https://gist.github.com/michaelcox/3800736 http://chaijs.com/plugins/chai-backbone

Updated on July 09, 2022

Comments

  • Mattias
    Mattias almost 2 years
    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();
        }
    });
    
  • Mattias
    Mattias almost 11 years
    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 function
  • Mattias
    Mattias almost 11 years
    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?
  • Martin Seeler
    Martin Seeler almost 11 years
    The method is "windowClosing" not "WindowClosing". You need to override it, so it has to be the exact name.
  • Maroun
    Maroun almost 11 years
    windowClosing and not WindowClosing with uppercase W.