Aaron Priesterroth
Aaron Priesterroth

Reputation: 317

JFrame close operation

How can I set another close operation for my JFrame besides the myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ? Example: I got a start() and a stop() method and I want the stop() to be called before the JFrame exits ( stop() saves my files and joins the Thread).

Upvotes: 1

Views: 1133

Answers (2)

c.s.
c.s.

Reputation: 4816

Add a WindowListener to your JFrame and implement the windowClosing() event:

// Use WindowAdapter if you don't want to implement the rest of the methods
// otherwise use new WindowListener()
window.addWindowListener( new WindowAdapter() { 
    @Override
    public void windowClosing(WindowEvent e) {
        stop();
    }});            

Upvotes: 3

Grammin
Grammin

Reputation: 12205

Override the dispose method.

@Override
public void dispose() {
  this.stop();
  super.dispose();
}

Upvotes: 1

Related Questions