Get the desired look and feel from a per-user preference : Preference « Development « Java Tutorial






/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;

import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LookAndFeelPrefs {
  public static final String PREF_NAME = "preferredLookAndFeelClassName";

  /**
   * Get the desired look and feel from a per-user preference. If the
   * preferences doesn't exist or is unavailable, use the default look and feel.
   * The preference is shared by all classes in the same package as prefsClass.
   */
  public static void setPreferredLookAndFeel(Class prefsClass) {
    Preferences prefs = Preferences.userNodeForPackage(prefsClass);
    String defaultLAF = UIManager.getSystemLookAndFeelClassName();
    String laf = prefs.get(PREF_NAME, defaultLAF);
    try {
      UIManager.setLookAndFeel(laf);
    } catch (Exception e) { // ClassNotFound or InstantiationException
      // An exception here is probably caused by a bogus preference.
      // Ignore it silently; the user will make do with the default LAF.
    }
  }

  /**
   * Create a menu of radio buttons listing the available Look and Feels. When
   * the user selects one, change the component hierarchy under frame to the new
   * LAF, and store the new selection as the current preference for the package
   * containing class c.
   */
  public static JMenu createLookAndFeelMenu(final Class prefsClass, final ActionListener listener) {
    // Create the menu
    final JMenu plafmenu = new JMenu("Look and Feel");

    // Create an object used for radio button mutual exclusion
    ButtonGroup radiogroup = new ButtonGroup();

    // Look up the available look and feels
    UIManager.LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();

    // Find out which one is currently used
    String currentLAFName = UIManager.getLookAndFeel().getClass().getName();

    // Loop through the plafs, and add a menu item for each one
    for (int i = 0; i < plafs.length; i++) {
      String plafName = plafs[i].getName();
      final String plafClassName = plafs[i].getClassName();

      // Create the menu item
      final JMenuItem item = plafmenu.add(new JRadioButtonMenuItem(plafName));
      item.setSelected(plafClassName.equals(currentLAFName));

      // Tell the menu item what to do when it is selected
      item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
          // Set the new look and feel
          try {
            UIManager.setLookAndFeel(plafClassName);
          } catch (UnsupportedLookAndFeelException e) {
            // Sometimes a Look-and-Feel is installed but not
            // supported, as in the Windows LaF on Linux platforms.
            JOptionPane.showMessageDialog(plafmenu, "The selected Look-and-Feel is "
                + "not supported on this platform.", "Unsupported Look And Feel",
                JOptionPane.ERROR_MESSAGE);
            item.setEnabled(false);
          } catch (Exception e) { // ClassNotFound or Instantiation
            item.setEnabled(false); // shouldn't happen
          }

          // Make the selection persistent by storing it in prefs.
          Preferences p = Preferences.userNodeForPackage(prefsClass);
          p.put(PREF_NAME, plafClassName);

          // Invoke the supplied action listener so the calling
          // application can update its components to the new LAF
          // Reuse the event that was passed here.
          listener.actionPerformed(event);
        }
      });

      // Only allow one menu item to be selected at once
      radiogroup.add(item);
    }

    return plafmenu;
  }
}








6.36.Preference
6.36.1.Put key value pair to Preference
6.36.2.Get childrenNames from Preferences
6.36.3.Get keys from Preferences
6.36.4.Get name and parent from Preference
6.36.5.Get node from Preference
6.36.6.Get value from Preferences
6.36.7.Getting and Setting Java Type Values in a Preference
6.36.8.Getting the Maximum Size of a Preference Key and Value
6.36.9.Getting the Roots of the Preference Trees
6.36.10.Removing a Preference from a Preference Node
6.36.11.Export Preferences to XML file
6.36.12.Preference save and load
6.36.13.Preferences Inspector
6.36.14.Removing a Preference Node
6.36.15.Determining If a Preference Node Exists
6.36.16.Determining If a Preference Node Contains a Specific Key
6.36.17.Determining If a Preference Node Contains a Specific Value
6.36.18.Read / write data in Windows registry
6.36.19.Retrieving the Parent and Child Nodes of a Preference Node
6.36.20.Exporting the Preferences in a Preference Node
6.36.21.Exporting the Preferences in a Subtree of Preference Nodes
6.36.22.Listening for Changes to Preference Values in a Preference Node
6.36.23.Determining When a Preference Node Is Added or Removed
6.36.24.Get the desired look and feel from a per-user preference