I'm writing a small Java application using Swing. The application also has a networking component.
I want to run some code before exiting(close channels, keys, etc).Is there a way to do this, without adding an Exit button in my interface, in which to do my cleanup, then do System.exit()?
You can use the Runtime#addShutdownHook function to add a function to run when the JVM shuts down.
You should be able to do what you want in windowClosing(), shown here. 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With