Working with UIDefaults.

New Articles

What is a session?.

Web Based FTP with PHP.

Java signed applet howto

In the previous step we retrieved the UIManager's default settings but found that the information is not in a human readable format. Now we will attempt to present these values into a format that you and I can easily interpret. Some of these settings will be colors, in such cases we will try to colorize a table cell. If the setting is an icon we will try to display that icon and so on.

For the purpose of displaying the settings we will use a JTable. The data is from a Hashtable since the UIDefaults class is it's subclass. Ideally we should create our own TableModel from the Hashtable contents but that leads to more lines of code and we all know that laziness is a virtue (at least for a programmer).

Our approach then is to create 2D array, with three columns. The first column would hold the key, the second column is for the class name of the value, it is the third column that holds the value. This array will be passed to the JTable constructor.

        UIDefaults def = UIManager.getLookAndFeelDefaults();
        Enumeration enum = def.keys();

        for(int i=0 ; enum.hasMoreElements() ; i++)
            Object item = enum.nextElement();
            Object value = def.get(item);
            tableData[i][0] = item;
            tableData[i][1] = (value == null) ? "" : value.getClass().getName();
            tableData[i][2] = (value == null) ? "" : def.get(item);
            System.out.println(item +" " + def.get(item));

That's what the code looks like in it's most basic form, but it still does not produce the desired result. Now we have to identify the type of each object and create a new instance accordingly (where applicable) so that a visual component that we can easily identify is created. To achieve this we get help from a TableCellRenderer.

 	else if(value instanceof IconUIResource)
                Icon ico = (Icon) value;
                lb.setMinimumSize(new Dimension(ico.getIconWidth(),ico.getIconHeight()));
                try {
                catch (ClassCastException ex) {
                    return cmp;
                catch (NullPointerException ex)
                return lb;

The listing above is a fragment of code from our TableCellRenderer, which is a subclass of the DefaultTableCellRenderer. Notice that we are using JLabel to provide a visual component. Sometimes the component that we wish to show may be in a different branch of the class hierarchy and if that happens it cannot be displayed in a table cell. Hence the need to watch out for Exceptions.

You can download the complete code and try it out. You can see it in action on the next page in the form of an applet.