Install Nimbus Look and get a feel for Java Tutorials - never get caught in a catch block to set a different look

The Java Tutorials give the following example of installing Nimbus Look and Feel: Java Tutorials - Nimbus Look and Feel

import javax.swing.UIManager.*;

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

      

It seems to me that the trick will only happen if it UIManager.setLookAndFeel()

throws an exception. If you put code to set a different look and feel in the catch block, it will theoretically never run as you only use .setLookAndFeel()

on the set LookAndFeel

s. If Nimbus is not available on the system, it will fall through ForEach and exit the try-catch block without setting anything.

Shouldn't you use code for "If nimbus is not available ..." followed after ForEach and before the catch? In the catch block you can specify the SystemLookAndFeel or DefaultLookAndFeel supplied with JAVA.

Am I correct with the try-catch block as above? I am concerned that I am missing something as this example code has been quoted many times and I have seen this question raised.

+3


source to share


1 answer


Nimbus is always available from the class level as it is included in every JRE since Java 1.5.

Which means "Not available" is possible if the system cannot load the object, due to any problem in the reflex mechanism.



Anyway, I would say that you should set a different Look-and-Feel in the block finally

. This way you can download alternatives even in lower Java versions.

0


source







All Articles