Press the [X] button to close the JFrame, will not call the delete method

I created a JFrame as follows:

public class XFrame extends JFrame {

public XFrame() {
    setSize(100, 100);
}
@Override
public void dispose() {
    super.dispose();
    System.out.println("Dispose get called");
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            XFrame f = new XFrame();
            f.setTitle("Hello World");
            //f.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            f.setDefaultCloseOperation(EXIT_ON_CLOSE);
            f.setVisible(true);
        }
    });
}
}

I expect that when I press the close button [X], the dispose method is called. However, this is a situation where DISPOSE_ON_CLOSE is set as DefaultCloseOperation (???). Java really surprises me here. How to implement a method that will be called in both cases with the DefaultCloseOperation values ​​(DISPOSE_ON_CLOSE and EXIT_ON_CLOSE)?

+3
source share
4 answers

EXIT_ON_CLOSE, , "" , JVM . "" , dispose, -, .

, Swing , Swing. , setSize() setLocation() setBounds() – 2 , ? , .

, - , , WindowListener windowClosing().

+5

WindowListener windowClosing()? , , .

: - , , - dispose(), windowClosing().

+1

dispose , . , ( javadoc), DISPOSE_ON_EXIT...
System.exit, EXIT_ON_CLOSE, , , - ( ).

EXIT_ON_CLOSE - , SecurityManager

0

, EXIT_ON_CLOSE JFrame.EXIT_ON_CLOSE?

0

Source: https://habr.com/ru/post/1725522/


All Articles