How to Use the New Look And Feel?


After exporting the new look and feel into a JAR file, you may want to use the new look and feel in your application, to do that, you should follow two steps:

 

Step 1: Add the Look And Feel JAR File into Your Classpath

You should add the look and feel JAR file into the classpath of your Java application. For example, your Java application can be launched with the following scripts:

(For Windows) java -cp junit.jar;jdom.jar com.yourdomain.SampleMainClass

(For Linux/Unix) java -cp junit.jar:jdom.jar com.yourdomain.SampleMainClass

After we appended the look and feel JAR file (let's use EaSynth Look And Feel JAR file as example), the scripts should looks like this:

(For Windows) java -cp junit.jar;jdom.jar;EaSynthLookAndFeel.jar com.yourdomain.SampleMainClass

(For Linux/Unix) java -cp junit.jar:jdom.jar:EaSynthLookAndFeel.jar com.yourdomain.SampleMainClass

Now the Java application can locate the new Java theme, but we still need to set the current look and feel of the application.

 

Step 2: Set the Current Look And Feel

We can do this via the UIManager or the VM argument:

(1) Via VM Argument

It is a straightforward way, you can specify the default look and feel by passing a special VM argument, that means you don't need to modify your source code at all. Here are examples for launching your application, set the EaSynth Look And Feel as the default look and feel by using the -Dswing.defaultlaf VM argument:

(For Windows)

java -Dswing.defaultlaf=com.easynth.lookandfeel.EaSynthLookAndFeel 
-cp junit.jar;jdom.jar;EaSynthLookAndFeel.jar com.yourdomain.SampleMainClass

(For Linux/Unix)

java -Dswing.defaultlaf=com.easynth.lookandfeel.EaSynthLookAndFeel 
-cp junit.jar:jdom.jar:EaSynthLookAndFeel.jar com.yourdomain.SampleMainClass


(2) Via UIManager

Also we can invoke the setLookAndFeel() static method in UIManager to set the current look and feel. Here are two examples to apply the EaSynth Look And Feel:

(Example 1: use the class name as the parameter for setLookAndFeel())

 try {
     UIManager.setLookAndFeel("com.easynth.lookandfeel.EaSynthLookAndFeel");
} catch (Exception e) {
e.printStackTrace(); }

(Example 2: use the look and feel object as the parameter for setLookAndFeel())

 import com.easynth.lookandfeel.EaSynthLookAndFeel; 
 try {
     final EaSynthLookAndFeel easynthLAF = new EaSynthLookAndFeel();
     UIManager.setLookAndFeel(easynthLAF);
 } catch (Exception e) {
     e.printStackTrace();
 }

The two examples are completely equivalent in runtime. The second example may give you the compile error when the "EaSynthLookAndFeel.jar" file is not present in the build path, thus you can check your classpath before running it.

Normally the UIManager.setLookAndFeel() method should be invoked at the very beginning of the main() method, so that the GUI elements created later will have the expected look and feel. But sometimes you may want to let user to choose a look and feel at runtime (just like what EaSynth Look And Feel Designer did), it is true that you can still invoke the UIManager.setLookAndFeel() method, but don't forget to invoke the updateComponentTreeUI() method to update the UI for all existing GUI components. Here is an example:

(Example 3: update the UIs for the given GUI component and its children, and children's children... So that they will have the new look and feel)

    SwingUtilities.updateComponentTreeUI(yourMainFrameInstance);

In the example above, the yourMainFrameInstance is the instance of the top-level frame of you application, so that all elements in your UI will be updated.

In most cases, applying a new Java theme to your application will be very easy, and you don't need to change any existing source code in your application. But sometimes you may meet some problems, it may happen when your existing source code specify some UI component's properties distinctly, such as the opaque property, which may conflict with the default settings from the new look and feel. Such kinds of issues can be fixed by changing the existing source code a little.