The Java Developers Almanac 1.4


Order this book from Amazon.

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

e413. Retrieving a Preference Node

A preference node can be retrieved using a Class object or by a string. When using a Class object to retrieve a preference node, the package containing the Class object identifies the node.

It is important to note that these methods also automatically create the node if it doesn't exist. Since nodes take up resources, even when empty, you should avoid creating unnecessary nodes. See e415 Determining If a Preference Node Exists for information on how to avoid creating nodes.

The string used to retrieve a preference node is called a path and resembles a file path. For example, the path for the preference node associated with the package com.mycompany is /com/mycompany.

Paths that begin with `/' are absolute paths. A relative path can be specified (one that does not begin with `/') and is treated relative to another preference node. For example, if you use the node at /javax/swing to retrieve another node with the path text/html, the node at /javax/swing/text/html is returned.

    // System preference nodes
    
    // Use a Class
    Preferences prefs = Preferences.systemNodeForPackage(java.lang.String.class);
    
    // Use an absolute path
    prefs = Preferences.systemRoot().node("/java/lang/String");
    
    // Use a relative path
    prefs = Preferences.systemRoot().node("/javax/swing");
    prefs = prefs.node("text/html");
    
    // User preference nodes
    
    // Use a class
    prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class);
    
    // Use an absolute path
    prefs = Preferences.userRoot().node("/com/mycompany");
    
    // Use a relative path
    prefs = Preferences.userRoot().node("/javax/swing");
    prefs = prefs.node("text/html");

 Related Examples
e411. Getting the Roots of the Preference Trees
e412. Creating a Preference Node
e414. Removing 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.