In my code, I try to set Preferences
. I have two input fields: JTextField
and JPasswordField
. JPasswordField
works fine, however JTextField
does not keep in memory the preference info, instead it copies the password info.
import java.util.prefs.Preferences;
import javax.swing.*;
public class TestJP {
public static Preferences userPreferences = Preferences.userRoot();
public final static String LOGIN_KEY = "";
public final static String PASSWORD_KEY = "";
public static void main(String[] args) {
JTextField login = new JTextField(20);
login.setText(userPreferences.get(LOGIN_KEY, ""));
JPasswordField password = new JPasswordField(20);
password.setText(userPreferences.get(PASSWORD_KEY, ""));
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("login:"));
myPanel.add(login);
myPanel.add(Box.createHorizontalStrut(15));
myPanel.add(new JLabel("password:"));
myPanel.add(password);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Login", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
userPreferences.put(LOGIN_KEY,login.getText());
userPreferences.put(PASSWORD_KEY, password.getText());
}
}
}
Does JPasswordField
override somehow the JTextField
?
Your keys are BOTH empty strings. They need to be unique strings.
Before:
public final static String LOGIN_KEY = "";
public final static String PASSWORD_KEY = "";
New:
public final static String LOGIN_KEY = "login_key";
public final static String PASSWORD_KEY = "password_key";