KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > utils > UIManagerUtils


1 /*
2  * $Id: UIManagerUtils.java,v 1.1 2005/02/24 20:35:25 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.swing.utils;
8
9 import java.lang.reflect.Method JavaDoc;
10 import javax.swing.UIManager JavaDoc;
11 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
12
13 /**
14  * Utility for working with the UIManager
15  * @author Richard Bair
16  */

17 public final class UIManagerUtils {
18     /**
19      * Hidden constructor
20      */

21     private UIManagerUtils() {
22     }
23     
24     /**
25      * Initializes the object in the UIDefaults denoted by 'key' to defaultObj <strong>only if</strong>
26      * the key is not already in the UIDefaults.
27      * @param key
28      * @param defaultObj
29      */

30     public static void initDefault(String JavaDoc key, Object JavaDoc defaultObj) {
31         Object JavaDoc obj = UIManager.get(key);
32         if (obj == null) {
33             UIManager.put(key, defaultObj);
34         }
35     }
36
37     /**
38      * Initializes the object in the UIDefaults denoted by 'key' to either the property in the metal look and feel
39      * associated with defaultMetalObjName, or the defaultObj if all else fails.
40      * @param key
41      * @param defaultMetalObjName
42      * @param defaultObj
43      */

44     public static void initDefault(String JavaDoc key, String JavaDoc defaultMetalObjName, Object JavaDoc defaultObj) {
45         Object JavaDoc obj = UIManager.get(key);
46         if (obj == null) {
47             try {
48                 Method JavaDoc m = ((MetalLookAndFeel JavaDoc)UIManager.getLookAndFeel()).getClass().getMethod(defaultMetalObjName, defaultObj.getClass());
49                 UIManager.put(key, m.invoke(UIManager.getLookAndFeel(), defaultMetalObjName));
50             } catch (Exception JavaDoc e) {
51                 UIManager.put(key, defaultObj);
52             }
53         }
54     }
55 }
56
Popular Tags