Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doing some "cleanup" before closing a program, how?

 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looked into the API for JFrame, my only choices are all constants with setDefaultCloseOperation().

So I looked into implementing WindowListener, but then I'd have to over-ride ALL the class's methods!

Isn't there another way for me to simply execute some code before the program exits? I just want to close sockets and streams and such.
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tarek Khojah:
Looked into the API for JFrame, my only choices are all constants with setDefaultCloseOperation().

So I looked into implementing WindowListener, but then I'd have to over-ride ALL the class's methods!

Isn't there another way for me to simply execute some code before the program exits? I just want to close sockets and streams and such.


Forgot about the adapter classes in a hurry did you?


Instead of using WindowListener use WindowAdapter so that you will need to override only windowClosing.
 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my class already inherits JFrame, I can't inherit two classes.

I implemented WindowListener and just had empty methods, as in the following:
methodName(){}, but I doubt that's the best solution.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create an anonymous inner class:
 
Justin Chu
Ranch Hand
Posts: 209
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or create an inner class. I find inner classes for listeners easier to manage.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tarek Khojah:
But my class already inherits JFrame, I can't inherit two classes.

I implemented WindowListener and just had empty methods, as in the following:
methodName(){}, but I doubt that's the best solution.


Listeners are interfaces. JFrame is a class. True, you can only extend one class but you can implement as many interfaces as you want.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tarek Khojah:
But my class already inherits JFrame, I can't inherit two classes.
There is most likely no reason for your class to inherit JFrame. You aren't overriding any of JFrame's methods, are you? If not then your class should look somewhat like this:
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
There is most likely no reason for your class to inherit JFrame. You aren't overriding any of JFrame's methods, are you?


Probably not but it's fairly common practice to extend JFrame anyway. Nearly all of Sun's Swing tutorial examples do this. I do the same thing with JPanels and JDialogs as well, never overriding any of their methods. It makes for cleaner more manageable code.

Just because I'm a car doesn't mean I want to change how many wheels I have. But I might want to hang some dice in the mirror.
[ September 28, 2007: Message edited by: Gregg Bolinger ]
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
it's fairly common practice to extend JFrame anyway.
Yes, I know that. But from the OO design point of view, it's the wrong thing to do. And the post I responded to shows why it's the wrong thing to do, and how it's easy to fix that.

The examples that have the class be a JFrame and implement half a dozen listeners are even worse.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing as the original question was about executing some code before a program is shut down, wouldn't Runtime.addShutdownHook(Thread) be easier than worrying about how the GUI might have to be adapted?
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Seeing as the original question was about executing some code before a program is shut down, wouldn't Runtime.addShutdownHook(Thread) be easier than worrying about how the GUI might have to be adapted?


Hi Ulf,
To confess, I, personally wasn't aware of this method. Thanks for mentioning it. I was wondering what would be the difference between the addShutdownHook and the runFinalization()? Would I achieve the same result if I override the finalize in the class which is extending the JFrame class, write my cleanup code there before calling super.finalize? Does garbage collection happen even if System.exit is called?
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Maneesh Godbole:

I was wondering what would be the difference between the addShutdownHook and the runFinalization()? Would I achieve the same result if I override the finalize in the class which is extending the JFrame class, write my cleanup code there before calling super.finalize? Does garbage collection happen even if System.exit is called?


Ok. I did the following. I extended the JFrame and added the shutdown hook.


I also overrode the finalize and called the same cleanup() from there.


This is the cleanup.


When I run the app and close the window I see that the cleanup from the hook is being called.
calling from hook
Cleaning up


So does it mean the finalize is not called because the jvm is exiting?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure about the details, but it makes sense that the GC would not be run -and thus finalizers not be called- when the JVM is about to be shut down. No need to clean up something that is soon to be thrown away...
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that shutdown hooks are not run if you kill the process, by using kill -9 in Unix or killing the java(w) process from the processes list in Windows.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that the shutdown hook is a VM shutdown event, for lack of a better term. If you need to do "cleanup" when a frame closes while not exiting the VM you'll need to use the java.awt.event window events as mentioned previously in this post.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that Java makes only one absolute guarantee regarding finalize(): that before an object is actually reclaimed by the garbage collector, its finalize() method will be called. That's it. This is absolutely not a guarantee that a finalize() method will ever be called -- if your program never runs out of memory, then the objects will never be reclaimed. Even the "runFinalizersOnExit()" functionality (which I thought was deprecated, anyway) is not an absolute guarantee -- just a "best effort" promise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic