Listening for Changes to Preference Values in a Preference Node - Java Native OS

Java examples for Native OS:Preference

Description

Listening for Changes to Preference Values in a Preference Node

Demo Code

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

public class Main {
  public static void main(String[] args) {
    Preferences prefs = Preferences.userNodeForPackage(String.class);

    prefs.addPreferenceChangeListener(new PreferenceChangeListener() {
      public void preferenceChange(PreferenceChangeEvent evt) {
        // Get the node that changed
        Preferences node = evt.getNode();

        String key = evt.getKey();

        String newValue = evt.getNewValue();
      }//from   ww w. j a  v  a  2 s.  co m
    });

    // Add a preference
    prefs.put("key", "a string");

    // Modify the preference
    prefs.put("key", "a new string");

    // Remove the preference
    prefs.remove("key");
  }
}

Result


Related Tutorials