src/share/classes/javax/swing/UIManager.java

Print this page




 564      *           class could not be found
 565      * @exception InstantiationException if a new instance of the class
 566      *          couldn't be created
 567      * @exception IllegalAccessException if the class or initializer isn't accessible
 568      * @exception UnsupportedLookAndFeelException if
 569      *          <code>lnf.isSupportedLookAndFeel()</code> is false
 570      * @throws ClassCastException if {@code className} does not identify
 571      *         a class that extends {@code LookAndFeel}
 572      */
 573     public static void setLookAndFeel(String className)
 574         throws ClassNotFoundException,
 575                InstantiationException,
 576                IllegalAccessException,
 577                UnsupportedLookAndFeelException
 578     {
 579         if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) {
 580             // Avoid reflection for the common case of metal.
 581             setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
 582         }
 583         else {
 584             Class lnfClass = SwingUtilities.loadSystemClass(className);
 585             setLookAndFeel((LookAndFeel)(lnfClass.newInstance()));
 586         }
 587     }
 588 
 589     /**
 590      * Returns the name of the <code>LookAndFeel</code> class that implements
 591      * the native system look and feel if there is one, otherwise
 592      * the name of the default cross platform <code>LookAndFeel</code>
 593      * class. This value can be overriden by setting the
 594      * <code>swing.systemlaf</code> system property.
 595      *
 596      * @return the <code>String</code> of the <code>LookAndFeel</code>
 597      *          class
 598      *
 599      * @see #setLookAndFeel
 600      * @see #getCrossPlatformLookAndFeelClassName
 601      */
 602     public static String getSystemLookAndFeelClassName() {
 603         String systemLAF = AccessController.doPrivileged(
 604                              new GetPropertyAction("swing.systemlaf"));


1032      *
1033      * @return <code>UIDefaults</code> from the current look and feel
1034      * @see #getDefaults
1035      * @see #setLookAndFeel(LookAndFeel)
1036      * @see LookAndFeel#getDefaults
1037      */
1038     public static UIDefaults getLookAndFeelDefaults() {
1039         maybeInitialize();
1040         return getLAFState().getLookAndFeelDefaults();
1041     }
1042 
1043     /**
1044      * Finds the Multiplexing <code>LookAndFeel</code>.
1045      */
1046     private static LookAndFeel getMultiLookAndFeel() {
1047         LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel;
1048         if (multiLookAndFeel == null) {
1049             String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel";
1050             String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName);
1051             try {
1052                 Class lnfClass = SwingUtilities.loadSystemClass(className);
1053                 multiLookAndFeel = (LookAndFeel)lnfClass.newInstance();
1054             } catch (Exception exc) {
1055                 System.err.println("UIManager: failed loading " + className);
1056             }
1057         }
1058         return multiLookAndFeel;
1059     }
1060 
1061     /**
1062      * Adds a <code>LookAndFeel</code> to the list of auxiliary look and feels.
1063      * The auxiliary look and feels tell the multiplexing look and feel what
1064      * other <code>LookAndFeel</code> classes for a component instance are to be used
1065      * in addition to the default <code>LookAndFeel</code> class when creating a
1066      * multiplexing UI.  The change will only take effect when a new
1067      * UI class is created or when the default look and feel is changed
1068      * on a component instance.
1069      * <p>Note these are not the same as the installed look and feels.
1070      *
1071      * @param laf the <code>LookAndFeel</code> object
1072      * @see #removeAuxiliaryLookAndFeel


1320     }
1321 
1322 
1323     /**
1324      * If the user has specified a default look and feel, use that.
1325      * Otherwise use the look and feel that's native to this platform.
1326      * If this code is called after the application has explicitly
1327      * set it's look and feel, do nothing.
1328      *
1329      * @see #maybeInitialize
1330      */
1331     private static void initializeDefaultLAF(Properties swingProps)
1332     {
1333         if (getLAFState().lookAndFeel != null) {
1334             return;
1335         }
1336 
1337         // Try to get default LAF from system property, then from AppContext
1338         // (6653395), then use cross-platform one by default.
1339         String lafName = null;
1340         HashMap lafData =

1341                 (HashMap) AppContext.getAppContext().remove("swing.lafdata");
1342         if (lafData != null) {
1343             lafName = (String) lafData.remove("defaultlaf");
1344         }
1345         if (lafName == null) {
1346             lafName = getCrossPlatformLookAndFeelClassName();
1347         }
1348         lafName = swingProps.getProperty(defaultLAFKey, lafName);
1349 
1350         try {
1351             setLookAndFeel(lafName);
1352         } catch (Exception e) {
1353             throw new Error("Cannot load " + lafName);
1354         }
1355 
1356         // Set any properties passed through AppContext (6653395).
1357         if (lafData != null) {
1358             for (Object key: lafData.keySet()) {
1359                 UIManager.put(key, lafData.get(key));
1360             }
1361         }
1362     }
1363 
1364 
1365     private static void initializeAuxiliaryLAFs(Properties swingProps)
1366     {
1367         String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey);
1368         if (auxLookAndFeelNames == null) {
1369             return;
1370         }
1371 
1372         Vector<LookAndFeel> auxLookAndFeels = new Vector<LookAndFeel>();
1373 
1374         StringTokenizer p = new StringTokenizer(auxLookAndFeelNames,",");
1375         String factoryName;
1376 
1377         /* Try to load each LookAndFeel subclass in the list.
1378          */
1379 
1380         while (p.hasMoreTokens()) {
1381             String className = p.nextToken();
1382             try {
1383                 Class lnfClass = SwingUtilities.loadSystemClass(className);
1384                 LookAndFeel newLAF = (LookAndFeel)lnfClass.newInstance();
1385                 newLAF.initialize();
1386                 auxLookAndFeels.addElement(newLAF);
1387             }
1388             catch (Exception e) {
1389                 System.err.println("UIManager: failed loading auxiliary look and feel " + className);
1390             }
1391         }
1392 
1393         /* If there were problems and no auxiliary look and feels were
1394          * loaded, make sure we reset auxLookAndFeels to null.
1395          * Otherwise, we are going to use the MultiLookAndFeel to get
1396          * all component UI's, so we need to load it now.
1397          */
1398         if (auxLookAndFeels.size() == 0) {
1399             auxLookAndFeels = null;
1400         }
1401         else {
1402             getLAFState().multiLookAndFeel = getMultiLookAndFeel();
1403             if (getLAFState().multiLookAndFeel == null) {




 564      *           class could not be found
 565      * @exception InstantiationException if a new instance of the class
 566      *          couldn't be created
 567      * @exception IllegalAccessException if the class or initializer isn't accessible
 568      * @exception UnsupportedLookAndFeelException if
 569      *          <code>lnf.isSupportedLookAndFeel()</code> is false
 570      * @throws ClassCastException if {@code className} does not identify
 571      *         a class that extends {@code LookAndFeel}
 572      */
 573     public static void setLookAndFeel(String className)
 574         throws ClassNotFoundException,
 575                InstantiationException,
 576                IllegalAccessException,
 577                UnsupportedLookAndFeelException
 578     {
 579         if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) {
 580             // Avoid reflection for the common case of metal.
 581             setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
 582         }
 583         else {
 584             Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
 585             setLookAndFeel((LookAndFeel)(lnfClass.newInstance()));
 586         }
 587     }
 588 
 589     /**
 590      * Returns the name of the <code>LookAndFeel</code> class that implements
 591      * the native system look and feel if there is one, otherwise
 592      * the name of the default cross platform <code>LookAndFeel</code>
 593      * class. This value can be overriden by setting the
 594      * <code>swing.systemlaf</code> system property.
 595      *
 596      * @return the <code>String</code> of the <code>LookAndFeel</code>
 597      *          class
 598      *
 599      * @see #setLookAndFeel
 600      * @see #getCrossPlatformLookAndFeelClassName
 601      */
 602     public static String getSystemLookAndFeelClassName() {
 603         String systemLAF = AccessController.doPrivileged(
 604                              new GetPropertyAction("swing.systemlaf"));


1032      *
1033      * @return <code>UIDefaults</code> from the current look and feel
1034      * @see #getDefaults
1035      * @see #setLookAndFeel(LookAndFeel)
1036      * @see LookAndFeel#getDefaults
1037      */
1038     public static UIDefaults getLookAndFeelDefaults() {
1039         maybeInitialize();
1040         return getLAFState().getLookAndFeelDefaults();
1041     }
1042 
1043     /**
1044      * Finds the Multiplexing <code>LookAndFeel</code>.
1045      */
1046     private static LookAndFeel getMultiLookAndFeel() {
1047         LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel;
1048         if (multiLookAndFeel == null) {
1049             String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel";
1050             String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName);
1051             try {
1052                 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1053                 multiLookAndFeel = (LookAndFeel)lnfClass.newInstance();
1054             } catch (Exception exc) {
1055                 System.err.println("UIManager: failed loading " + className);
1056             }
1057         }
1058         return multiLookAndFeel;
1059     }
1060 
1061     /**
1062      * Adds a <code>LookAndFeel</code> to the list of auxiliary look and feels.
1063      * The auxiliary look and feels tell the multiplexing look and feel what
1064      * other <code>LookAndFeel</code> classes for a component instance are to be used
1065      * in addition to the default <code>LookAndFeel</code> class when creating a
1066      * multiplexing UI.  The change will only take effect when a new
1067      * UI class is created or when the default look and feel is changed
1068      * on a component instance.
1069      * <p>Note these are not the same as the installed look and feels.
1070      *
1071      * @param laf the <code>LookAndFeel</code> object
1072      * @see #removeAuxiliaryLookAndFeel


1320     }
1321 
1322 
1323     /**
1324      * If the user has specified a default look and feel, use that.
1325      * Otherwise use the look and feel that's native to this platform.
1326      * If this code is called after the application has explicitly
1327      * set it's look and feel, do nothing.
1328      *
1329      * @see #maybeInitialize
1330      */
1331     private static void initializeDefaultLAF(Properties swingProps)
1332     {
1333         if (getLAFState().lookAndFeel != null) {
1334             return;
1335         }
1336 
1337         // Try to get default LAF from system property, then from AppContext
1338         // (6653395), then use cross-platform one by default.
1339         String lafName = null;
1340         @SuppressWarnings("unchecked")
1341         HashMap<Object, String> lafData =
1342                 (HashMap) AppContext.getAppContext().remove("swing.lafdata");
1343         if (lafData != null) {
1344             lafName = lafData.remove("defaultlaf");
1345         }
1346         if (lafName == null) {
1347             lafName = getCrossPlatformLookAndFeelClassName();
1348         }
1349         lafName = swingProps.getProperty(defaultLAFKey, lafName);
1350 
1351         try {
1352             setLookAndFeel(lafName);
1353         } catch (Exception e) {
1354             throw new Error("Cannot load " + lafName);
1355         }
1356 
1357         // Set any properties passed through AppContext (6653395).
1358         if (lafData != null) {
1359             for (Object key: lafData.keySet()) {
1360                 UIManager.put(key, lafData.get(key));
1361             }
1362         }
1363     }
1364 
1365 
1366     private static void initializeAuxiliaryLAFs(Properties swingProps)
1367     {
1368         String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey);
1369         if (auxLookAndFeelNames == null) {
1370             return;
1371         }
1372 
1373         Vector<LookAndFeel> auxLookAndFeels = new Vector<LookAndFeel>();
1374 
1375         StringTokenizer p = new StringTokenizer(auxLookAndFeelNames,",");
1376         String factoryName;
1377 
1378         /* Try to load each LookAndFeel subclass in the list.
1379          */
1380 
1381         while (p.hasMoreTokens()) {
1382             String className = p.nextToken();
1383             try {
1384                 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1385                 LookAndFeel newLAF = (LookAndFeel)lnfClass.newInstance();
1386                 newLAF.initialize();
1387                 auxLookAndFeels.addElement(newLAF);
1388             }
1389             catch (Exception e) {
1390                 System.err.println("UIManager: failed loading auxiliary look and feel " + className);
1391             }
1392         }
1393 
1394         /* If there were problems and no auxiliary look and feels were
1395          * loaded, make sure we reset auxLookAndFeels to null.
1396          * Otherwise, we are going to use the MultiLookAndFeel to get
1397          * all component UI's, so we need to load it now.
1398          */
1399         if (auxLookAndFeels.size() == 0) {
1400             auxLookAndFeels = null;
1401         }
1402         else {
1403             getLAFState().multiLookAndFeel = getMultiLookAndFeel();
1404             if (getLAFState().multiLookAndFeel == null) {