Preferences: addPreferenceChangeListener(PreferenceChangeListener pcl) : Preferences « java.util.prefs « Java by API






Preferences: addPreferenceChangeListener(PreferenceChangeListener pcl)

   
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;

public class Main implements PreferenceChangeListener {
  private Preferences userPrefs;

  public static final String NAMEPREF = "name";

  public static final String EMAILPREF = "email";

  public static void main(String args[]) {
    new Main();
  }

  public Main() {
    userPrefs = Preferences.userNodeForPackage(Main.class);

    System.out.println(userPrefs.get(NAMEPREF, ""));
    System.out.println(userPrefs.get(EMAILPREF, ""));

    userPrefs.put(NAMEPREF, "name");
    userPrefs.put(EMAILPREF, "email");
    Preferences.userNodeForPackage(Main.class).addPreferenceChangeListener(this);
  }

  public void preferenceChange(PreferenceChangeEvent evt) {
    String key = evt.getKey();
    String val = evt.getNewValue();
    if (key.equals(NAMEPREF)) {
      System.out.println(val);
    } else if (key.equals(EMAILPREF)) {
      System.out.println(val);
    }
  }
}

   
    
    
  








Related examples in the same category

1.Preferences.MAX_KEY_LENGTH
2.Preferences.MAX_VALUE_LENGTH
3.Preferences: absolutePath()
4.Preferences: exportNode(OutputStream os)
5.Preferences: exportSubtree(OutputStream os)
6.Preferences: get(String key, String def)
7.Preferences: getBoolean(String key, boolean def)
8.Preferences: getByteArray(String key, byte[] def)
9.Preferences: getDouble(String key, double def)
10.Preferences: getFloat(String key, float def)
11.Preferences: getInt(String key, int def)
12.Preferences: getLong(String key, long def)
13.Preferences: importPreferences(InputStream is)
14.Preferences: keys()
15.Preferences: name()
16.Preferences: node(String pathName)
17.Preferences: nodeExists(String pathName)
18.Preferences: parent()
19.Preferences: put(String key, String value)
20.Preferences: putBoolean(String key, boolean value)
21.Preferences: putByteArray(String key, byte[] value)
22.Preferences: putDouble(String key, double value)
23.Preferences: putFloat(String key, float value)
24.Preferences: putLong(String key, long value)
25.Preferences: remove(String key)
26.Preferences: removeNode()
27.Preferences: systemRoot()
28.Preferences: userNodeForPackage(Class c)
29.Preferences: userRoot()