When developing JavaFX applications, it’s essential to provide a seamless and personalized user experience. One way to achieve this is by saving and retrieving user preferences, such as settings, configurations, or recently used data. Java provides a convenient API called java.util.prefs that allows us to store and retrieve user preferences effortlessly. In this article, we’ll explore how to use Java Preferences to save and restore the window size and position of your JavaFX application automatically, providing a seamless and pleasant user experience.
What are Java Preferences?
Java Preferences is part of the java.util.prefs package, and it provides a simple way to store user preferences. These preferences are stored in a platform-specific manner, typically using the system registry on Windows, .plist files on macOS, and configuration files on Linux. The Java Preferences API abstracts these details, making it easy to save and retrieve user-specific data without worrying about the underlying storage mechanism.
Saving and Restoring Window Size and Position
The main focus of this article is to save and restore the window size and position of a JavaFX application. We will achieve this by using the Java Preferences API to store the window dimensions and coordinates and then retrieve them the next time the application is launched.
Saving Window Size and Position
To save the window size and position, we need to store four values: the width and height of the window, as well as its left and top positions. Let’s see how we can do that:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.prefs.Preferences;
public class Main extends Application {
private static final String PREFERENCE_NODE_NAME = "com.coderscratchpad.javafx.preferences";
private static final String WIDTH_KEY = "width";
private static final String HEIGHT_KEY = "height";
private static final String LEFT_KEY = "left";
private static final String TOP_KEY = "top";
private double windowWidth;
private double windowHeight;
private double windowLeft;
private double windowTop;
private final StackPane parent = new StackPane();
@Override
public void start(Stage stage) throws Exception {
this.setupStage(stage);
}
private void setupStage(Stage stage) {
// Load the saved window dimensions and positions
loadWindowSettings();
Scene scene = new Scene(this.parent);
// Sets the stage title
stage.setTitle("JavaFX Preferences: Saving and Retrieving User Preferences");
// Sets the stage scene
stage.setScene(scene);
// Set the stage size
stage.setWidth(windowWidth);
stage.setHeight(windowHeight);
// Set the stage position
stage.setX(this.windowLeft);
stage.setY(this.windowTop);
// Save the window size and position when the application is closed
stage.setOnCloseRequest(event -> {
saveWindowSettings(stage.getWidth(), stage.getHeight(), stage.getX(), stage.getY());
});
// Show stage on screen
stage.show();
}
private void loadWindowSettings() {
Preferences preferences = Preferences.userRoot().node(PREFERENCE_NODE_NAME);
this.windowWidth = preferences.getDouble(WIDTH_KEY, 640.0);
this.windowHeight = preferences.getDouble(HEIGHT_KEY, 480.0);
this.windowLeft = preferences.getDouble(LEFT_KEY, 100.0);
this.windowTop = preferences.getDouble(TOP_KEY, 100.0);
}
private void saveWindowSettings(double width, double height, double left, double top) {
Preferences preferences = Preferences.userRoot().node(PREFERENCE_NODE_NAME);
preferences.putDouble(WIDTH_KEY, width);
preferences.putDouble(HEIGHT_KEY, height);
preferences.putDouble(LEFT_KEY, left);
preferences.putDouble(TOP_KEY, top);
}
}
In the Main class, we define four constants: WIDTH_KEY, HEIGHT_KEY, LEFT_KEY, and TOP_KEY, which will be used as keys when storing and retrieving the window size and position from the preferences.
In the loadWindowSettings() method, we retrieve the saved values from the preferences. If no values are found (i.e., the application is launched for the first time), default values will be used for the window size and position.
In the saveWindowSettings(double width, double height, double left, double top) method, we save the current window size and position to the preferences. This method is called when the application is closed.
Applying the Saved Window Size and Position
We’ve already seen how to save the window size and position, but how do we apply these saved values when the application starts? The answer lies in the setupStage() method of the Main class:
private void setupStage(Stage stage) {
// Load the saved window dimensions and positions
loadWindowSettings();
Scene scene = new Scene(this.parent);
// Sets the stage title
stage.setTitle("JavaFX Preferences: Saving and Retrieving User Preferences");
// Sets the stage scene
stage.setScene(scene);
// Set the stage size
stage.setWidth(windowWidth);
stage.setHeight(windowHeight);
// Set the stage position
stage.setX(this.windowLeft);
stage.setY(this.windowTop);
// Save the window size and position when the application is closed
stage.setOnCloseRequest(event -> {
saveWindowSettings(stage.getWidth(), stage.getHeight(), stage.getX(), stage.getY());
});
// Show stage on screen
stage.show();
}
By calling the setWidth(), setHeight(), setX(), and setY() methods on the stage, we set the window size and position according to the values retrieved from the preferences. This ensures that the window will appear exactly where and with the size the user had it during the last session.
Conclusion
In this article, we’ve learned how to use Java Preferences to save and restore the window size and position of a JavaFX application. By implementing these simple preferences, you can significantly improve the user experience of your application, making it feel more tailored to each individual user.
Saving and restoring the window size and position is just one of the many ways you can utilize Java Preferences to provide a personalized and seamless experience to your users. So go ahead and implement this functionality in your JavaFX applications and see how much your users will appreciate the thoughtfulness you’ve put into making their interaction with your software a joy!
I hope you found this code informative and useful. If you would like to receive more content, please consider subscribing to our newsletter!
There is no javafx.util.Prefs API. Seems you’re refering to java.prefs module? Your approach works fine until user changes screen resolution and ends up with offscreen app. I’ve found that third-party com.iamsoft.util.ui.javafx.prefs package makes this stuff way easier. “StagePrefUtil.manage(stage, Main.class)” is all you need.
The article intended to demonstrate storing and retrieving user preferences, not focusing solely on the stage size and location. The latter was just an example that came to mind during the writing process. Additionally, preferences could be utilized for storing fonts and other settings. And yeah I meant to use the java.util.prefs package, but in JavaFX. The JavaFX application used the java.util.prefs package for storing and retrieving the stage size and location as shown in the article. But that’s good too, I love your suggestion, I appreciate that, man. Thank you!