Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Setting Global Icons

The TreeTest application presented in the previous section displayed a tree containing information about a music library. The tree structure presented the data in a nicely structured visual. However, the file system icons used in the tree make the component somewhat less than ideal. Fortunately, the JFC provides a couple of techniques that can be used to specify the icons that are used in the tree. If the icons are to be changed for each tree that is going to be used in the application, changing the user interface properties for the tree icons is most appropriate. If you need each tree in your application to have custom icons, or the icon is based on the node’s contents rather than its position in the tree, a custom TreeCellRenderer is appropriate. This section demonstrates the former technique. A custom cell renderer will be shown in the next section.

The current look-and-feel determines the default icons used in the tree. However, the application can override the default values by changing properties in the UIManager. A complete description of the UIManager is presented in Chapter 30, "Pluggable Look-and-Feel." For this discussion, it is sufficient to understand that the UIManager contains a Dictionary that maps user interface properties to their value. These values are queried by the component and used in its display. For the JTree component, there are properties for the various icons displayed in the tree. The list of properties is presented in Table 11.2.

Table 11.2 Icon Properties for the JTree Class

Property Name Usage

Tree.openIcon When a non-leaf node is expanded
Tree.closedIcon When a non-leaf node is collapsed
Tree.leafIcon For a leaf node
Tree.expandedIcon Icon for the expanded control
Tree.collapsedIcon Icon for the collapsed control

The main method for the TreeTest application can be modified, as shown in Listing 11.10, to register the icons desired for your application with the UIManager. Then any JTree instance created in the application will use the new icons. The modified tree containing the custom images is shown in Figure 11.7.

Listing 11.10 Registering Custom Images in the TreeTest Application

public static void main( String args[] ) {

        Icon albumIcon = new ImageIcon( "album.gif" );
        Icon albumsIcon = new ImageIcon( "albums.gif" );
        Icon expandedIcon = new ImageIcon( "TreeExpanded.gif" );
        Icon collapsedIcon = new ImageIcon( "TreeCollapsed.gif" );
        UIDefaults defaults = UIManager.getDefaults();
        defaults.put( "Tree.openIcon", openIcon );
        defaults.put( "Tree.closedIcon", closedIcon );
        defaults.put( "Tree.leafIcon", leafIcon );
        defaults.put( "Tree.expandedIcon", expandedIcon );
        defaults.put( "Tree.collapsedIcon", collapsedIcon );

        TreeTest treeTest = new TreeTest();
        JFrame frame = new ApplicationFrame( "Tree Test" );
        frame.getContentPane().add( treeTest.createTreePanel(),
                                    BorderLayout.CENTER );
        frame.pack();
        frame.setVisible( true );

    } // main


Figure 11.7  Custom icons in the TreeTest application.

Using a Custom Tree Renderer

The example in the previous section showed how to globally replace a tree’s icon in an application. This technique will suffice for many applications; but when complete control over a tree’s icons is required, a custom renderer for the tree must be provided. If the granularity for specifying icons in a tree described in the previous section suffices, a DefaultTreeCellRenderer instance can be customized and set for the tree. The DefaultTreeCellRenderer class contains methods for setting the opened, closed, and leaf icons, as well as methods for setting the text color and font. If this level of customization meets your requirements, there is no need to create your own renderer class.

When you need different icons for nodes that are at the same level in the hierarchy, a custom renderer class will have to be written. You saw earlier in this chapter that a renderer for a JTree instance must implement the TreeCellRenderer interface. The AlbumTreeRenderer class, shown in Listing 11.11, extends the JLabel class and implements the TreeCellRenderer interface. In the class’s constructor, it sets its opaque property to true. This is required in renderers that paint the background color of the cell. The AlbumTreeRenderer needs to paint the background color to show selected items in the tree. The renderer is bound to the tree by adding the following line of code in the createTreePanel method of the TreeTest application after the tree has been created:

tree.setCellRenderer( new AlbumTreeRenderer() );

For a renderer to display different icons for different nodes, it requires information about the nodes. This can be achieved by using many techniques. The method presented here is if the user object in a node is an instance of the AlbumCollectionItem class, its getIcon method is called to obtain the icon for the node. To make the renderer a bit more generic, an interface can define the getIcon method, and this would be checked for instead of the AlbumCollectionItem class in the renderer.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.