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

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Development » Preference 
6. 36. 24. Get the desired look and feel from a per-user preference
/*
 * 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.