PLAF and UIDefaults

More Articles

PHP Java Extension.

JSP File upload progress bar.

Signed Applet How-to

The Problem

It is supposedly quite a simple task to change the look and feel of java applets and programs with the plugable look and feel architecture of J2SE. That is only partially correct because even though PLAF allows you to choose between several existing UIs beyond that your options are limited. The recently introduced synth look and feel does allow you a measure of customizability but java 5 is not yet popular and the process is more complex than it seems.

Since laziness is considered virtue you wouldn't want to create your own look and feel. So what options remain?

The solution

Fonts, colors and icons associated with each L&F are stored in an instance of UIDefaults. You can change a given UI by making modifications to the values stored in these objects. The UIDefaults associated with a particular Look and Feel can be retrieved with a call to the static getLookAndFeelDefaults() method of the UIManager class.

In the latter parts of this article we will attempt to customize the installed L&Fs of swing by changing these default values. The approach that we take is somewhat akin to the using themes with the metal look and feel. Before we go any further we need to know that the defaults settings are and this bit of code will retrieve this information for us:


  import javax.swing.*;
  import java.util.*;
  public class UIProperties {
	public static void main(String[] args) {
		new UIProperties();
		UIDefaults def = UIManager.getLookAndFeelDefaults();
		Enumeration enum = def.keys();
		while (enum.hasMoreElements()) {
			Object item = enum.nextElement();
			System.out.println(item +" " + def.get(item));
		}
    	}
  } 


UIDefaults is a subclass of Hashtable as such, we can use the standard methods of retrieving key-value pairs from a hash table to produce what appears to be a rather long list. Though some of the items are not human readable, it's easy to see which setting should change if we wanted to change the default font of JLabel or default background of JPanel as an example.

In the next step we will attempt to convert the output produced by the code snippet above into a more human readable format. If you have compiled and run executed this code let's move on.