0

Basically it's a client program with a GUI so I want to close the sockets when the user closes the client program. Is there is Listener or something that will allow me to do this?

Faahmed
  • 375
  • 4
  • 21
  • Aside: what exactly do you hope to accomplish by changing the close operation for a window *after* it's closed? Or even just before it's closing? Usually it's enough to call `setDefaultCloseOperation()` at the start of your program. – millimoose May 05 '13 at 20:17
  • Well, I did that only because syb0rg below suggested it. – Faahmed May 05 '13 at 20:47
  • If all you want to do is clean up when the application is closed, try taking a look at [Shitdown hook](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)) – MadProgrammer May 05 '13 at 21:00
  • @MadProgrammer, sorry I'm a noob. I'll need an example to understand. – Faahmed May 05 '13 at 21:19
  • @AnAlien For example you could read the javadocs. Or google for "java shutdown hook example". – millimoose May 05 '13 at 21:21
  • 1
    @AnAlien Start by having a look at the link provided (sorry about the spelling :P) – MadProgrammer May 05 '13 at 21:29

3 Answers3

2
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        // do stuff
    }
});

Note that this will only be called when the default close operation has been set to EXIT_ON_CLOSE before the frame is closed via the (x) button. The default is HIDE_ON_CLOSE which technically does not close the window, therefore the listener would not be notified.

Marco
  • 1,430
  • 10
  • 18
  • 1
    I'm not sure if windowClosed will ever get called under these circumstances, windowClosing is a safer bet - needs to be tested – MadProgrammer May 05 '13 at 21:02
  • @MadProgrammer Should work because the only difference should be when the listener is notified (before or after the frame is gone), but just to be safe I changed it ;) – Marco May 05 '13 at 21:07
  • From experince, I tend to avoid windowClosed, because I think the JVM is closed before it is called, when using EXIT_ON_CLOSE, I could've wrong, been a while since I did things ths way ;) – MadProgrammer May 05 '13 at 21:14
  • Even thought it didn't work for me, everyone else said basically the same thing except you were first so I select you as the right answer. Thanks for trying. I'm posting a new question regarding this code and how to make it actually work. – Faahmed May 06 '13 at 02:58
2

Add a WindowListener for the closing event:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent e)
    {
        // Do stuff
    }
});

For more help, look at this tutorial on WindowListener's.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
  • You have to set the *default close operation* to *do nothing* on close. – Maroun May 05 '13 at 19:56
  • @MarounMaroun, what is the parameter for nothing? – Faahmed May 05 '13 at 19:57
  • @MarounMaroun Not in all cases. From the code I took this from, I set the close operation to `JFrame.EXIT_ON_CLOSE` right before I add this window listener, and it works perfectly. You could also set the close operation within the `WindowClosing` event (I edited this into the answer). – syb0rg May 05 '13 at 19:58
  • So I have a class which extends JFrame so I did: `this.addWindow......` And inside the windowClosed method, to test it out I have a println statement. But it doesn't get printed out when I close the JFrame. – Faahmed May 05 '13 at 19:59
  • @AnAlien No, look at my edited answer to try and get a better understanding. – syb0rg May 05 '13 at 20:02
  • @AnAlien Change `this.setDefaultCloseOperation` to `frame.setDefaultCloseOperation`. – syb0rg May 05 '13 at 20:08
  • But frame isn't a variable. I'm doing this from within the constructor which is why I'm using this – Faahmed May 05 '13 at 20:09
  • @AnAlien You have to make a new `JFrame` object somewhere. Whatever you named that object, that is what you have to set the close operation for. – syb0rg May 05 '13 at 20:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29450/discussion-between-an-alien-and-syb0rg) – Faahmed May 05 '13 at 20:13
  • [without jokes](http://stackoverflow.com/a/9228595/714968), resutl could be == not to required something to change, lets this job ro WindowListener – mKorbel May 05 '13 at 20:39
  • 1
    @mKorbel I'm sorry, I don't understand that. – Faahmed May 05 '13 at 20:48
  • see my code example linked in my comment, WindowListener to override events (about JFrame, JFrame has three buttons) from buttons, by using this code you can to try any possible variations, but logics could be JFrame.setVisible(false), then whatever code, if code ended then to call System.exit – mKorbel May 05 '13 at 20:53
  • 2
    I'm not sure if changing the default close operation AFTER the window has begun closing is going to do anything... – MadProgrammer May 05 '13 at 21:01
  • @MadProgrammer I found some example online somewhere that had it like that, and I followed it and it seemed to work. I've updated my answer. – syb0rg May 05 '13 at 21:23
  • 1
    @syb0rg As I said, I wasn't sure, it just seems very...weird to try and change the state of the operation mid stream, that's all – MadProgrammer May 05 '13 at 21:32
1

To refer to this from an enclosing scope, use this:

class MyFrame extends JFrame {

    public MyFrame() {
        this.addWindowListener(
            // omitting AIC boilerplate

            // Use the name of the enclosing class
            MyFrame.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // ...
        }
    }
}

Or store it in a variable with a different name:

class MyFrame extends JFrame {

    public MyFrame() {
        final JFrame thisFrame = this;
        this.addWindowListener(
            // omitting AIC boilerplate

            // Use the name of the enclosing class
            thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // ...
        }
    }
}
millimoose
  • 39,073
  • 9
  • 82
  • 134
  • I updated my main code and nothing happens when I close JFrame. – Faahmed May 05 '13 at 20:20
  • @AnAlien You edited your question to be a completely different one than the one I answered. This is "moving the goalposts", it's a behaviour that's frowned upon, don't do this. Work on the new problem yourself, and post a new question when stuck. (The new question should explain what you're trying to accomplish for instance.) – millimoose May 05 '13 at 21:21
  • Well, the problem still is the same. It's just that I'm trying out everyone's different answers and none seem to be working. I can't get anything to happen when user closes JFrame. – Faahmed May 05 '13 at 22:02
  • Just to clarify, I'm still trying to accomplish the same thing which is to perform an action once user closes/Xs JFrame. – Faahmed May 05 '13 at 22:03
  • @AnAlien No, that's just the task you're trying to ultimately accomplish. But that's not the problem your original question described, and your edit changed it *substantially*. SO should be about resolving isolated problems, not to guide you through your work until it's complete. – millimoose May 05 '13 at 22:51
  • I understand now, sorry. I'll just rollback and post a new question. – Faahmed May 06 '13 at 02:56