Ali
Ali

Reputation: 10453

No action perform when Window Frame closed in Java

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent b) {

            // Do stuff
            if (socket != null) {

                    socket.close();
                    socket = null;

                } catch (IOException e) {
                    socket = null;
                }
                System.out.println("About to abort!");

            }
            dispose();

        }

    });

From the above code what I'm trying to achive is that when they click on the red X button it will check if the TCP socket is still connect or not, and if it still connecting then close(); and assign a null to the socket, and I want to print something, but nothing really happen at all.

Am I doing something wrong here?

Please advice.

Thanks

Upvotes: 1

Views: 129

Answers (1)

Reimeus
Reimeus

Reputation: 159784

Ensure that the close operation is set to DO_NOTHING_ON_CLOSE:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Upvotes: 2

Related Questions