3

This has been asked here: How to persist JavaFX GUI State? And has the tag javafx-2. But no answers.

Gonna ask again and put in more tags.

I'm looking for something like what was done in JSR 296: Swing Application Framework where the demo application remembers it's window size, location etc. E.g if the user maximized the window, and then closes, opens the program again, the window is maximized.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Thirdy
  • 235
  • 3
  • 10
  • Here is a simple example of [using a `Properties` file to persist the Swing frame size](http://stackoverflow.com/a/7778332/418556). – Andrew Thompson Sep 24 '15 at 09:39
  • Yeah I'm doing that actually. I have this combobox that I want it to persist it's state: [screenshot](https://cloud.githubusercontent.com/assets/75921/10032589/81fdfbc6-61b6-11e5-9eb7-df72f8d3f49c.png). I'm using java.util.Properties for this – Thirdy Sep 24 '15 at 14:19
  • 2
    *"I have this combobox that I want it to persist it's state:"* So.. go for it. – Andrew Thompson Sep 24 '15 at 14:21

1 Answers1

5

You can store basic information using the Preferences API.

Depending on the operating system, the preferences are saved in different places (for example in Windows, they are stored in the registry file).

Typical use goes like this:

  • To store a value:

    Preferences prefs = Preferences.userNodeForPackage(MyClass.class);
    prefs.putBoolean("isWindowMaximized", true);
    
  • To get a value:

    Preferences prefs = Preferences.userNodeForPackage(MyClass.class);
    boolean value = prefs.getBoolean("isWindowMaximized", false);
    

You will find more information about this API in this Oracle tutorial.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Is it a good thing that the application messes around with the registry? But I'll check it out, hopefully it's can be configured to save into a properties file instead. Otherwise I guess it's a bit of an improvement over plain java.util.Properties – Thirdy Sep 24 '15 at 14:25
  • but not like Swing Application Framework. where the ui state persistence is done automatically – Thirdy Sep 25 '15 at 10:37