The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.prefs  [18 examples] > Nodes  [7 examples]

e414. Removing a Preference Node

Removing a preference node will remove all of the node's descendants.

See also e415 Determining If a Preference Node Exists.

    try {
        // First check to see if the node is already removed;
        // otherwise, getting the node will automatically create it
        boolean exists = Preferences.userRoot().nodeExists("/foo"); // false
    
        if (!exists) {
            // Get the node
            Preferences prefs = Preferences.userRoot().node("/foo");
    
            // Remove the node
            prefs.removeNode();
    
            // Trying to remove it again would cause an IllegalStateException
            //prefs.removeNode();
        }
    
        // Create a node with a child
        Preferences prefs = Preferences.userRoot().node("/foo/child");
        exists = Preferences.userRoot().nodeExists("/foo");       // true
        exists = Preferences.userRoot().nodeExists("/foo/child"); // true
    
        // Remove the parent node
        Preferences.userRoot().node("/foo").removeNode();
    
        // Both parent and child are removed
        exists = Preferences.userRoot().nodeExists("/foo");       // false
        exists = Preferences.userRoot().nodeExists("/foo/child"); // false
    } catch (BackingStoreException e) {
    }

 Related Examples
e411. Getting the Roots of the Preference Trees
e412. Creating a Preference Node
e413. Retrieving a Preference Node
e415. Determining If a Preference Node Exists
e416. Retrieving the Parent and Child Nodes of a Preference Node
e417. Finding a Preference in a Preference Tree

See also: Events    Importing and Exporting   


© 2002 Addison-Wesley.