Team LiB
Previous Section Next Section

Package java.util.prefs

Java 1.4

The java.util.prefs package contains classes and interfaces for managing persistant user and system-wide preferences for Java applications and classes. Most applications will use only the Preferences class itself. Some will also use the event objects and listener interfaces defined by this package, and some may need to explicitly catch the types of exceptions defined by this package. Application programmers never need to use the PreferencesFactory interface or the AbstractPreferences class, which are intended for Preferences implementors only.

To use the Preferences class, first use a static method to obtain an appropriate Preferences object or objects, and then use a get( ) method to query a preference value or a put( ) method to set a preference value. The code below shows a typical usage. See the Preferences class for details.

import java.util.prefs.Preferences;
public class TextEditor {
  // some constants that define default values for preferences
  public static final int WIDTH_DEFAULT = 80;
  public static final String DICTIONARY_DEFAULT = "";
  // Fields to be initialized from preference values
  public int width;             // Screen width in columns
  public String dictionary;     // Dictionary name for spell-checking
  public void initPrefs( ) {
    // Get Preferences objects for user and system preferences for this package
    Preferences userprefs = Preferences.userNodeForPackage(TextEditor.class);
    Preferences sysprefs = Preferences.systemNodeForPackage(TextEditor.class);
    // Look up preference values.  Note that we always pass a default value
    width = userprefs.getInt("width", WIDTH_DEFAULT);
    // Look up a user preference using a system preference as the default
    dictionary = userprefs.get("dictionary",
                               sysprefs.get("dictionary",
                                            DICTIONARY_DEFAULT));
  }
}

Interfaces

public interface NodeChangeListener extends java.util.EventListener;
public interface PreferenceChangeListener extends java.util.EventListener;
public interface PreferencesFactory;

Events

public class NodeChangeEvent extends java.util.EventObject;
public class PreferenceChangeEvent extends java.util.EventObject;

Other Classes

public abstract class Preferences;
   public abstract class AbstractPreferences extends Preferences;

Exceptions

public class BackingStoreException extends Exception;
public class InvalidPreferencesFormatException extends Exception;

    Team LiB
    Previous Section Next Section