This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Where to read/write properties in Swing application? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Where to read/write properties in Swing application?" Watch "Where to read/write properties in Swing application?" New topic
Author

Where to read/write properties in Swing application?

Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 99
Hell, it is about 5 years ago I posted on this forum and haven't done any Java programming since 2 years.... But I have a new project again, and a very trivial problem.

I work on a program with a Swing GUI, and according to the guidelines, I start the GUI in the event dispatch thread. My main class is not GUI aware, I consider it as good practice to have aa non-Swing aware main class but maybe this idea is a bit obsolete...


Not very special, the issue is that the constructor of the main class returns immediately and the main object gets out of scope. Even the main() methods returns immediately so the main thread ends.

So there is not really a good place to place code to save the property file when the aplication is closed. Is the finalize() method maybe a good location? I do not think so, nor do I think the reading and writing of the property file belongs in the GUI classes.... In each case the reading and writing of the property file shoud occur in the same class I guess.
Obviously this is a trivial problem and solved more than a thousand times What do I miss here?
REgards, Klaas


"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Runtime.getRuntime.addShutdownHook(...)

This will always be called when the JVM shuts down except when:
  • you call Runtime.getRuntime().halt(...)
  • the JVM is forcefully terminated (using sigkill in Linux or from the processes tab of Windows' Task Manager). Terminating through sigclose (Linux) or from the applications tab of Windows' Task Manager does call them, as far as I know.
  • although I'm not 100% sure, I also believe shutdown hooks are not called when the JVM is terminated because Windows / Linux shuts down completely.


  • SCJP 1.4 - SCJP 6 - SCWCD 5
    How To Ask Questions How To Answer Questions
    Klaas van Gelder
    Ranch Hand

    Joined: Jul 08, 2004
    Posts: 99
    Thanks, this works indeed! Never seen that before.... I wonder if this is indeed the way most programs handle the writing of persistent data in standalone applications.
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Where to read/write properties in Swing application?
     
    Similar Threads
    Basic invokeLater question
    Properties/Settings - a design question
    I wrote my own FileMonitor. Check it out.
    Tomcat config properties file
    Swing repaint() not working / might be a case of RFM / tried a few things but can't get it to work