Two Steps to Integrate New Java Look And Feel into Your Applications

Author: Xavier YOUNG (Xavier.YOUNG@easynth.com)

Date: 2008.07.14

Source: http://www.easynth.com


Integrate the New Look And Feel to Your Application

We can develop a brand new look and feel by using the EaSynth Look And Feel Designer, you can preview the UI effect on most Java GUI applications by using this IDE, but you still need to integrate the new Java look and feel into your products before you sell them.

The IDE will pack the new Java theme as a stand alone JAR file, which can be integrated in your applications very easily. This article will introduce the two steps to perform the integration. In some cases, you can even apply the new look and feel to your applications without changing your source code.

 

Step One: Append the Look And Feel JAR File into Your Classpath

First of all, you should append the look and feel JAR file into the classpath of your 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 new Java theme is available in your application, we still need to tell the application to use the new Java theme.

 

Step Two: Specify the Current Look And Feel

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

(1) Via VM Argument

It is a very straightforward way, you can specify the current look and feel by passing the VM argument, that means you don't need to modify your source code at all. Here are examples for launching your application, specifying the EaSynth Look And Feel via 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

We can also 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 on runtime. The second example may give you the compile error when the "EaSynthLookAndFeel.jar" file is not present in the build path.

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 anytime (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.

 

Some Remarks

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.

You are welcome to contact us when you found some issues that can not be solved, you can send mail to our support team: support@easynth.com