Are you sure that name
variable has the same value? Because it should return correct stored value - maybe value is null?
Try printing them out just before storing & getting.
You can also try to flush preferences after storing:
prefs.flush();
If you're on Windows you can check if the preferences are stored in regedit
under
\HKEY_CURRENT_USER\Software\JavaSoft\Prefs\<your package path>
in Linux I'm not sure but there should be a hidden folder in your home directory. Something like
~/.java/.prefs/<package path>

I've developed a JFrame using preferences for storing last position & size of a frame. You can look at it here:
RememberableFrame
The library is at:
java-utils
The piece of code that might interest you:
public void saveSize() {
Preferences preferences = Preferences.userNodeForPackage(this.getClass());
preferences.put(getId() + X_KEY, String.valueOf(getLocation().x));
preferences.put(getId() + Y_KEY, String.valueOf(getLocation().y));
preferences.put(getId() + W_KEY, String.valueOf(getSize().width));
preferences.put(getId() + H_KEY, String.valueOf(getSize().height));
preferences.put(getId() + MAX_KEY,
String.valueOf((getExtendedState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH));
try {
preferences.flush();
}
catch(BackingStoreException e) {
e.printStackTrace();
}
}
public void setSavedSize() {
Preferences preferences = Preferences.userNodeForPackage(this.getClass());
String xs = preferences.get(getId() + X_KEY, "");
String ys = preferences.get(getId() + Y_KEY, "");
String ws = preferences.get(getId() + W_KEY, "");
String hs = preferences.get(getId() + H_KEY, "");
String max = preferences.get(getId() + MAX_KEY, "");
if(max != null && !max.trim().isEmpty() && Boolean.valueOf(max) == true) {
setDefaultSize();
setExtendedState(JFrame.MAXIMIZED_BOTH);
return;
}
if(xs.length() == 0 || ys.length() == 0 || ws.length() == 0 || hs.length() == 0) {
setDefaultSize();
}
else {
sizeFromPreferences = true;
int x = Integer.parseInt(xs);
int y = Integer.parseInt(ys);
int w = Integer.parseInt(ws);
int h = Integer.parseInt(hs);
setLocation(x, y);
setSize(w, h);
}
}
EDIT
If you want to create some kind of settings storing system you can use the same convention as in RememberableFrame
- use prefixes.
In RememberableFrame
I use:
private String getId() {
return this.getClass().getSimpleName() + id;
}
where id
is custom String provided by the developer or an empty string. But remember that the key of the property you want to set has length limit.
From the API:
static int MAX_KEY_LENGTH
Maximum length of string allowed as a key (80 characters).
static int MAX_NAME_LENGTH
Maximum length of a node name (80 characters).
static int MAX_VALUE_LENGTH
Maximum length of string allowed as a value (8192 characters).
You might also consider using classes specifically for "storing settings" purposes. For example class KeyboardSettings
and even in different packages.