aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Doing some Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Doing some "cleanup" before closing a program, how?" Watch "Doing some "cleanup" before closing a program, how?" New topic
Author

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

Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
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

Joined: Jul 26, 2007
Posts: 10376
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

Create an anonymous inner class:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
Or create an inner class. I find inner classes for listeners easier to manage.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 41872
    
  63
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?


Ping & DNS - my free Android networking tools app
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

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

Joined: Jul 26, 2007
Posts: 10376
    
    8

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
Marshal

Joined: Mar 22, 2005
Posts: 41872
    
  63
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
Consider Paul's rocket mass heater.
 
subject: Doing some "cleanup" before closing a program, how?