5.6. Look and Feel
Last updated: 25 January 2013.
This tutorial explains how to use different look and feels in Java.
Swing allows you to customize a GUI by changing the look and feel (L&F) as you like. A given L&F displays the GUI in a particular flavour.The look defines the general appearance of components and the feel defines their behaviour (for example, the highlighting when the mouse hovers a component). Several L&Fs are provided by the JDK. You can choose a L&F from the ones provided by the JDK or create your own L&F or just set the L&F to the native system's L&F. By default, the L&F is set to the Swing L&F (also known as the Metal L&F).
L&F creation is not covered in this tutorial because it is quite challenging and I think it is not worth the effort. Instead, this tutorial will show you how to set the L&F to one of the available L&Fs on your system, which generally produces satisfactory results.
L&Fs are subclasses of the class LookAndFeel. Each L&F is identified by its fully qualified class name. For example, the class name javax.swing.plaf.metal.MetalLookAndFeel refers to the default Swing L&F which is also known as the Metal L&F. Note that the default Swing L&F or Metal L&F is sometimes simply called the Java L&F.
5.6.1. What are the available L&Fs on your system?
At least, the two following L&Fs are available on every system:
- The default Swing L&F or Metal L&F: It tends to look the same on all systems. If you do not set the L&F explicitly, the default Swing L&F is used except on Apple systems which use the native Apple L&F by default.
- The native system's L&F: With this L&F, components look different on different systems. Of course, it is not possible to set the L&F of a Windows computer to a Linux native system's L&F and vice versa.
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class App {
public static void main(String[] args){
for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
System.out.println(info.getClassName());
}
}
import javax.swing.UIManager.LookAndFeelInfo;
public class App {
public static void main(String[] args){
for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
System.out.println(info.getClassName());
}
}
Here are snapshots of several L&Fs available on a Mac OS X system:
Swing Metal |
Nimbus |
Motif |
5.6.2. How to set the L&F?
The L&F can be set in 3 different ways:
Programmatically:
To set the L&F programmatically, just call the method setLookAndFeel(String className) or setLookAndFeel(LookAndFeel newLookAndFeel) provided by the class UIManager. The first method takes a fully qualified class name and the second method takes a subclass of the class LookAndFeel. The call to setLookAndFeel must be done before instanciating any Swing class, otherwise the default Swing L&F will be loaded. As an example, the following statement sets the L&F to the Nimbus L&F:
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch(Exception e){
e.printStackTrace();
}
If the Nimbus L&F is not available on your system, an exception is thrown and the L&F is set to the default L&F.
As I said before, Apple computers use their native system's L&F as the default L&F. If you are a Mac user and want to set the L&F to the Swing default L&F know as the Metal L&F, you can do it in 3 ways:
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.setLookAndFeel(new MetalLookAndFeel());
Conversely, the default L&F for computers running Windows or Linux is the Swing default L&F. If you want to set the L&F to the native system's L&F, you can call the method setLookAndFeel(String className) like this:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Note that you can also change the L&F after your GUI has been displayed. All you have to do is change the L&F and then call the static method updateComponentTreeUI(Component c) provided by the class SwingUtilities for the change to take effect. The argument of the method updateComponentTreeUI(Component c) is a top level container. Hence, the method must be invoked for each top level container of your application.Using the command line:
You can set the L&F by specifying a command-line argument at startup this way:
java -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel App
App is the class containing the code that starts your application (the main method). In the above command, the swing.defaultlaf property is set to com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel.Using the swing.properties file:
You can set the swing.defaultlaf property within a file named swing.properties located in the lib subdirectory of your JDK installation directory. If the file does not exist, you have to create it. For example, the swing.properties file on a Mac computer may contain the following line:
swing.defaultlaf=com.apple.laf.AquaLookAndFeel
5.6.4. How to use third-party L&Fs?
This tutorial discussed different ways of setting the L&F to one of the available L&Fs on your system. However, you can download and use a third-party L&F as well. All you need to do is add the downloaded L&F to your classpath and use it as any other available L&F on your system.
You are here :
JavaPerspective.com >
Intermediate Tutorials >
5. Graphical User Interfaces >
5.6. Look and Feel
Next tutorial : JavaPerspective.com > Intermediate Tutorials > 5. Graphical User Interfaces > 5.7. Borders
Next tutorial : JavaPerspective.com > Intermediate Tutorials > 5. Graphical User Interfaces > 5.7. Borders