Igor Tupitsyn Igor Tupitsyn - 2 months ago 5x
Java Question

Java: saved settings with Preferences not restored when a GUI application starts

In my Java app, I have a number of saved values and settings that I would like to get as soon as the application starts. All these values are supposed to be stored in a singleton.
For example, I have a JDialog, where I save a path to my file using this:

AppSingleton appSingleton = AppSingleton.getInstance( );
private Preferences prefs;

private void btnSetPanelSetNavdataActionPerformed(java.awt.event.ActionEvent evt) {


prefs = Preferences.userRoot().node(this.getClass().getName());
prefs.put("NAVDATA_PATH", txtNavdataPath.getText());


I am checking and the value is saved.

This is my singleton:

public class AppSingleton

private static AppSingleton instance = null;
private String navdataPath = "";

private AppSingleton()


public static AppSingleton getInstance()
if(instance == null)
instance = new AppSingleton();
return instance;

public String getNavdataPath() {
return navdataPath;

public void setNavdataPath(String navdataPath) {
this.navdataPath = navdataPath;

public void initDefaultValues()
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
this.navdataPath = prefs.get("NAVDATA_PATH","");

System.out.printf("CHECK DEFAULT: %s",this.navdataPath);

I am trying to get the saved values from the main() method in the class that initializes the JFrame:

public static void main(String args[]) {

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable()
public void run() {
new IGAMainFrame().setVisible(true);

/* HERE!!! */
AppSingleton appSingleton = AppSingleton.getInstance();

But the values are not initialized from the Preference class. "CHECK DEFAULT" shows empty string. I do not understand why and whether there is a better practice of restoring all saved values when a GUI application starts.



Change the line prefs = Preferences.userRoot().node(this.getClass().getName()); inside btnSetPanelSetNavdataActionPerformed to prefs = Preferences.userRoot().node(Appsingleton.class.getName()); Inside button click method, this.getClass() is not AppSingleton