Java - Only dispose JFrame on close?

15,458

This works for me:

public class Test {

    public static class TestFrame implements Runnable{
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(200, 300);
            frame.setVisible(true);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new TestFrame());
        EventQueue.invokeLater(new TestFrame());
    }
}
Share:
15,458
Connor
Author by

Connor

Updated on June 15, 2022

Comments

  • Connor
    Connor almost 2 years

    In my Java program I would like that, regardless of any other windows being open, whenever the user clicks the red X in the corner, just that swing frame closes. I experimented with JFrame.DISPOSE_ON_CLOSE and other window listeners, but when I exit one JFrame the system still exited.

    To clarify, suppose I have two JFrames visible, exiting one automatically exits the other, and the system exits. Any ideas as to how to only close one JFrame? Thanks.