The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.prefs  [18 examples] > Events  [2 examples]

e422. Determining When a Preference Node Is Added or Removed

A NodeChangeEvent is fired when a preference node gets a new child node or loses a child node. This event is only guaranteed to fire if the listener and modifier are in the same application.

See also e421 Listening for Changes to Preference Values in a Preference Node.

    // Retrieve the user preference node for the package java.lang
    Preferences prefs = Preferences.userNodeForPackage(String.class);
    
    // Register the listener
    prefs.addNodeChangeListener(new NodeChangeListener() {
        public void childAdded(NodeChangeEvent evt) {
            // Get the node with the new child
            Preferences parent = evt.getParent();
    
            // Get the newly added child
            Preferences child = evt.getChild();
        }
        public void childRemoved(NodeChangeEvent evt) {
            // Get the node whose child was removed
            Preferences parent = evt.getParent();
    
            // Get the removed child
            Preferences child = evt.getChild();
        }
    });
    
    // Add a child preference node
    Preferences child = prefs.node("new node");
    
    try {
        // Remove the child preference node
        child.removeNode();
    
        // Remove current node; this does not fire a NodeChangeEvent
        prefs.removeNode();
    } catch (BackingStoreException e) {
    }

 Related Examples
e421. Listening for Changes to Preference Values in a Preference Node

See also: Importing and Exporting    Nodes   


© 2002 Addison-Wesley.