509 * underlying platform
510 *
511 * @see LookAndFeel#getName
512 * @see LookAndFeel#isSupportedLookAndFeel
513 *
514 * @since 9
515 */
516 public static LookAndFeel createLookAndFeel(String name)
517 throws UnsupportedLookAndFeelException {
518 Objects.requireNonNull(name);
519
520 if ("GTK look and feel".equals(name)) {
521 name = "GTK+";
522 }
523
524 try {
525 for (LookAndFeelInfo info : installedLAFs) {
526 if (info.getName().equals(name)) {
527 Class<?> cls = Class.forName(UIManager.class.getModule(),
528 info.getClassName());
529 LookAndFeel laf = (LookAndFeel) cls.newInstance();
530 if (!laf.isSupportedLookAndFeel()) {
531 break;
532 }
533 return laf;
534 }
535 }
536 } catch (InstantiationException | IllegalAccessException ignore) {
537 }
538
539 throw new UnsupportedLookAndFeelException(name);
540 }
541
542 /**
543 * Sets the current look and feel to {@code newLookAndFeel}.
544 * If the current look and feel is {@code non-null} {@code
545 * uninitialize} is invoked on it. If {@code newLookAndFeel} is
546 * {@code non-null}, {@code initialize} is invoked on it followed
547 * by {@code getDefaults}. The defaults returned from {@code
548 * newLookAndFeel.getDefaults()} replace those of the defaults
549 * from the previous look and feel. If the {@code newLookAndFeel} is
550 * {@code null}, the look and feel defaults are set to {@code null}.
551 * <p>
552 * A value of {@code null} can be used to set the look and feel
553 * to {@code null}. As the {@code LookAndFeel} is required for
554 * most of Swing to function, setting the {@code LookAndFeel} to
555 * {@code null} is strongly discouraged.
556 * <p>
608 * @exception InstantiationException if a new instance of the class
609 * couldn't be created
610 * @exception IllegalAccessException if the class or initializer isn't accessible
611 * @exception UnsupportedLookAndFeelException if
612 * <code>lnf.isSupportedLookAndFeel()</code> is false
613 * @throws ClassCastException if {@code className} does not identify
614 * a class that extends {@code LookAndFeel}
615 */
616 public static void setLookAndFeel(String className)
617 throws ClassNotFoundException,
618 InstantiationException,
619 IllegalAccessException,
620 UnsupportedLookAndFeelException
621 {
622 if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) {
623 // Avoid reflection for the common case of metal.
624 setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
625 }
626 else {
627 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
628 setLookAndFeel((LookAndFeel)(lnfClass.newInstance()));
629 }
630 }
631
632 /**
633 * Returns the name of the <code>LookAndFeel</code> class that implements
634 * the native system look and feel if there is one, otherwise
635 * the name of the default cross platform <code>LookAndFeel</code>
636 * class. This value can be overriden by setting the
637 * <code>swing.systemlaf</code> system property.
638 *
639 * @return the <code>String</code> of the <code>LookAndFeel</code>
640 * class
641 *
642 * @see #setLookAndFeel
643 * @see #getCrossPlatformLookAndFeelClassName
644 */
645 public static String getSystemLookAndFeelClassName() {
646 String systemLAF = AccessController.doPrivileged(
647 new GetPropertyAction("swing.systemlaf"));
648 if (systemLAF != null) {
1076 * @return <code>UIDefaults</code> from the current look and feel
1077 * @see #getDefaults
1078 * @see #setLookAndFeel(LookAndFeel)
1079 * @see LookAndFeel#getDefaults
1080 */
1081 public static UIDefaults getLookAndFeelDefaults() {
1082 maybeInitialize();
1083 return getLAFState().getLookAndFeelDefaults();
1084 }
1085
1086 /**
1087 * Finds the Multiplexing <code>LookAndFeel</code>.
1088 */
1089 private static LookAndFeel getMultiLookAndFeel() {
1090 LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel;
1091 if (multiLookAndFeel == null) {
1092 String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel";
1093 String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName);
1094 try {
1095 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1096 multiLookAndFeel = (LookAndFeel)lnfClass.newInstance();
1097 } catch (Exception exc) {
1098 System.err.println("UIManager: failed loading " + className);
1099 }
1100 }
1101 return multiLookAndFeel;
1102 }
1103
1104 /**
1105 * Adds a <code>LookAndFeel</code> to the list of auxiliary look and feels.
1106 * The auxiliary look and feels tell the multiplexing look and feel what
1107 * other <code>LookAndFeel</code> classes for a component instance are to be used
1108 * in addition to the default <code>LookAndFeel</code> class when creating a
1109 * multiplexing UI. The change will only take effect when a new
1110 * UI class is created or when the default look and feel is changed
1111 * on a component instance.
1112 * <p>Note these are not the same as the installed look and feels.
1113 *
1114 * @param laf the <code>LookAndFeel</code> object
1115 * @see #removeAuxiliaryLookAndFeel
1116 * @see #setLookAndFeel
1410
1411 private static void initializeAuxiliaryLAFs(Properties swingProps)
1412 {
1413 String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey);
1414 if (auxLookAndFeelNames == null) {
1415 return;
1416 }
1417
1418 Vector<LookAndFeel> auxLookAndFeels = new Vector<LookAndFeel>();
1419
1420 StringTokenizer p = new StringTokenizer(auxLookAndFeelNames,",");
1421 String factoryName;
1422
1423 /* Try to load each LookAndFeel subclass in the list.
1424 */
1425
1426 while (p.hasMoreTokens()) {
1427 String className = p.nextToken();
1428 try {
1429 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1430 LookAndFeel newLAF = (LookAndFeel)lnfClass.newInstance();
1431 newLAF.initialize();
1432 auxLookAndFeels.addElement(newLAF);
1433 }
1434 catch (Exception e) {
1435 System.err.println("UIManager: failed loading auxiliary look and feel " + className);
1436 }
1437 }
1438
1439 /* If there were problems and no auxiliary look and feels were
1440 * loaded, make sure we reset auxLookAndFeels to null.
1441 * Otherwise, we are going to use the MultiLookAndFeel to get
1442 * all component UI's, so we need to load it now.
1443 */
1444 if (auxLookAndFeels.size() == 0) {
1445 auxLookAndFeels = null;
1446 }
1447 else {
1448 getLAFState().multiLookAndFeel = getMultiLookAndFeel();
1449 if (getLAFState().multiLookAndFeel == null) {
1450 auxLookAndFeels = null;
|
509 * underlying platform
510 *
511 * @see LookAndFeel#getName
512 * @see LookAndFeel#isSupportedLookAndFeel
513 *
514 * @since 9
515 */
516 public static LookAndFeel createLookAndFeel(String name)
517 throws UnsupportedLookAndFeelException {
518 Objects.requireNonNull(name);
519
520 if ("GTK look and feel".equals(name)) {
521 name = "GTK+";
522 }
523
524 try {
525 for (LookAndFeelInfo info : installedLAFs) {
526 if (info.getName().equals(name)) {
527 Class<?> cls = Class.forName(UIManager.class.getModule(),
528 info.getClassName());
529 LookAndFeel laf =
530 (LookAndFeel) cls.getDeclaredConstructor().newInstance();
531 if (!laf.isSupportedLookAndFeel()) {
532 break;
533 }
534 return laf;
535 }
536 }
537 } catch (ReflectiveOperationException |
538 IllegalArgumentException ignore) {
539 }
540
541 throw new UnsupportedLookAndFeelException(name);
542 }
543
544 /**
545 * Sets the current look and feel to {@code newLookAndFeel}.
546 * If the current look and feel is {@code non-null} {@code
547 * uninitialize} is invoked on it. If {@code newLookAndFeel} is
548 * {@code non-null}, {@code initialize} is invoked on it followed
549 * by {@code getDefaults}. The defaults returned from {@code
550 * newLookAndFeel.getDefaults()} replace those of the defaults
551 * from the previous look and feel. If the {@code newLookAndFeel} is
552 * {@code null}, the look and feel defaults are set to {@code null}.
553 * <p>
554 * A value of {@code null} can be used to set the look and feel
555 * to {@code null}. As the {@code LookAndFeel} is required for
556 * most of Swing to function, setting the {@code LookAndFeel} to
557 * {@code null} is strongly discouraged.
558 * <p>
610 * @exception InstantiationException if a new instance of the class
611 * couldn't be created
612 * @exception IllegalAccessException if the class or initializer isn't accessible
613 * @exception UnsupportedLookAndFeelException if
614 * <code>lnf.isSupportedLookAndFeel()</code> is false
615 * @throws ClassCastException if {@code className} does not identify
616 * a class that extends {@code LookAndFeel}
617 */
618 public static void setLookAndFeel(String className)
619 throws ClassNotFoundException,
620 InstantiationException,
621 IllegalAccessException,
622 UnsupportedLookAndFeelException
623 {
624 if ("javax.swing.plaf.metal.MetalLookAndFeel".equals(className)) {
625 // Avoid reflection for the common case of metal.
626 setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
627 }
628 else {
629 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
630 try {
631 LookAndFeel laf =
632 (LookAndFeel)lnfClass.getDeclaredConstructor().newInstance();
633 setLookAndFeel(laf);
634 } catch (ReflectiveOperationException | IllegalArgumentException e) {
635 InstantiationException ex =
636 new InstantiationException("Wrapped Exception");
637 ex.initCause(e);
638 throw ex;
639 }
640 }
641 }
642
643 /**
644 * Returns the name of the <code>LookAndFeel</code> class that implements
645 * the native system look and feel if there is one, otherwise
646 * the name of the default cross platform <code>LookAndFeel</code>
647 * class. This value can be overriden by setting the
648 * <code>swing.systemlaf</code> system property.
649 *
650 * @return the <code>String</code> of the <code>LookAndFeel</code>
651 * class
652 *
653 * @see #setLookAndFeel
654 * @see #getCrossPlatformLookAndFeelClassName
655 */
656 public static String getSystemLookAndFeelClassName() {
657 String systemLAF = AccessController.doPrivileged(
658 new GetPropertyAction("swing.systemlaf"));
659 if (systemLAF != null) {
1087 * @return <code>UIDefaults</code> from the current look and feel
1088 * @see #getDefaults
1089 * @see #setLookAndFeel(LookAndFeel)
1090 * @see LookAndFeel#getDefaults
1091 */
1092 public static UIDefaults getLookAndFeelDefaults() {
1093 maybeInitialize();
1094 return getLAFState().getLookAndFeelDefaults();
1095 }
1096
1097 /**
1098 * Finds the Multiplexing <code>LookAndFeel</code>.
1099 */
1100 private static LookAndFeel getMultiLookAndFeel() {
1101 LookAndFeel multiLookAndFeel = getLAFState().multiLookAndFeel;
1102 if (multiLookAndFeel == null) {
1103 String defaultName = "javax.swing.plaf.multi.MultiLookAndFeel";
1104 String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName);
1105 try {
1106 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1107 multiLookAndFeel =
1108 (LookAndFeel)lnfClass.getDeclaredConstructor().newInstance();
1109 } catch (Exception exc) {
1110 System.err.println("UIManager: failed loading " + className);
1111 }
1112 }
1113 return multiLookAndFeel;
1114 }
1115
1116 /**
1117 * Adds a <code>LookAndFeel</code> to the list of auxiliary look and feels.
1118 * The auxiliary look and feels tell the multiplexing look and feel what
1119 * other <code>LookAndFeel</code> classes for a component instance are to be used
1120 * in addition to the default <code>LookAndFeel</code> class when creating a
1121 * multiplexing UI. The change will only take effect when a new
1122 * UI class is created or when the default look and feel is changed
1123 * on a component instance.
1124 * <p>Note these are not the same as the installed look and feels.
1125 *
1126 * @param laf the <code>LookAndFeel</code> object
1127 * @see #removeAuxiliaryLookAndFeel
1128 * @see #setLookAndFeel
1422
1423 private static void initializeAuxiliaryLAFs(Properties swingProps)
1424 {
1425 String auxLookAndFeelNames = swingProps.getProperty(auxiliaryLAFsKey);
1426 if (auxLookAndFeelNames == null) {
1427 return;
1428 }
1429
1430 Vector<LookAndFeel> auxLookAndFeels = new Vector<LookAndFeel>();
1431
1432 StringTokenizer p = new StringTokenizer(auxLookAndFeelNames,",");
1433 String factoryName;
1434
1435 /* Try to load each LookAndFeel subclass in the list.
1436 */
1437
1438 while (p.hasMoreTokens()) {
1439 String className = p.nextToken();
1440 try {
1441 Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
1442 LookAndFeel newLAF =
1443 (LookAndFeel)lnfClass.getDeclaredConstructor().newInstance();
1444 newLAF.initialize();
1445 auxLookAndFeels.addElement(newLAF);
1446 }
1447 catch (Exception e) {
1448 System.err.println("UIManager: failed loading auxiliary look and feel " + className);
1449 }
1450 }
1451
1452 /* If there were problems and no auxiliary look and feels were
1453 * loaded, make sure we reset auxLookAndFeels to null.
1454 * Otherwise, we are going to use the MultiLookAndFeel to get
1455 * all component UI's, so we need to load it now.
1456 */
1457 if (auxLookAndFeels.size() == 0) {
1458 auxLookAndFeels = null;
1459 }
1460 else {
1461 getLAFState().multiLookAndFeel = getMultiLookAndFeel();
1462 if (getLAFState().multiLookAndFeel == null) {
1463 auxLookAndFeels = null;
|