![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Listing 11.11 The AlbumTreeRenderer Class package com.foley.utility; import java.awt.Component; import javax.swing.*; import javax.swing.tree.*; /** * A tree renderer that knows about the record * collection tree. * <p> * @author Mike Foley **/ public class AlbumTreeRenderer extends JLabel implements TreeCellRenderer, AlbumTypes { /** * AlbumTreeRenderer, Constructor. * <p> * The renderer must be opaque so the background * color is painted. **/ public AlbumTreeRenderer() { setFont( UIManager.getFont( "Tree.font" ) ) ; setOpaque( true ); } /** * Configures the renderer based on the passed in components. * Text for the cell is obtained from the toString() method of * the value parameter. * <p> * Foreground and background colors are obtained from the * UIManager. * <p> * If the value is a DefaultMutableTreeNode, and its user object * is an AlbumCollectionItem, the icon is obtained from the item * and set for the cell. **/ public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus ) { // // Set the text for the cell. // if( value != null ) setText( value.toString() ); else setText( "" ); // // Set the colors for the cell. // if( selected ) { setBackground( UIManager.getColor( "Tree.selectionBackground" ) ); setForeground( UIManager.getColor( "Tree.selectionForeground" ) ); } else { setBackground( UIManager.getColor( "Tree.textBackground" ) ); setForeground( UIManager.getColor( "Tree.textForeground" ) ); } // else // // The type of the node is stored in the user object // in the mutable tree node. // setIcon( null ); if( value instanceof DefaultMutableTreeNode ) { DefaultMutableTreeNode node = ( DefaultMutableTreeNode )value; Object o = node.getUserObject(); if( o instanceof AlbumCollectionItem ) { AlbumCollectionItem albumItem = ( AlbumCollectionItem )o; setIcon( albumItem.getIcon() ); } } return( this ); } } // AlbumTreeRenderer The createTreeModel method in the TreeTest application must be updated to specify AlbumCollectionItem instances as the user object for each node in the tree where an icon is desired. In the original tree model, the user object for each node was a String containing the name of the node. This is changed to an AlbumCollectionItem instance with the same name and an appropriate icon. Examples of this change for the top of the tree are shown in the following code fragment: DefaultMutableTreeNode albums = new DefaultMutableTreeNode( new AlbumCollectionItem( "Albums", albumsIcon ) ); DefaultMutableTreeNode cds = new DefaultMutableTreeNode( new AlbumCollectionItem( "CDs", cdsIcon ) ); DefaultMutableTreeNode tapes = new DefaultMutableTreeNode( new AlbumCollectionItem( "Tapes", tapesIcon ) ); The AlbumCollectionItem class is very simple. It binds the String name and icon together. Since the DefaultMutableTreeNode class calls the toString method of its user object when its toString method is called, the AlbumCollectionItem class returns its name in its toString method. This will ensure that the name is displayed properly in the tree. The icon specified in the constructor is returned from the getIcon method. The complete class is shown in Listing 11.12. Listing 11.12 The AlbumCollectionItem Class package com.foley.utility; import javax.swing.*; /** * The AlbumCollectionItem class binds a name * and icon together. * <p> * @author Mike Foley **/ public class AlbumCollectionItem { String name; Icon icon; /** * AlbumCollectionItem, Constructor * <p> * @param name The name of this item. * @param icon The icon for this item. **/ public AlbumCollectionItem( String name, Icon icon ) { this.name = name; this.icon = icon; } /** * @return The name of this item. **/ public String getName() { return( name ); } /** * @return The icon associated with this item. **/ public Icon getIcon() { return( icon ); } /** * @return The String representation of this item. **/ public String toString() { return( name ); } } // AlbumCollectionItem The new tree created with the custom renderer is shown in Figure 11.8. Notice how different icons are used for siblings in the tree. To achieve this effect, a custom renderer is required. The renderer could further customize the tree by changing fonts and colors for each node.
SummaryThe JTree component is a complex control that provides tremendous capabilities. To take full advantage of these capabilities, a substantial number of classes and interfaces must be understood. This chapter presented the convenience methods in the JTree class that allow a tree to be constructed by using standard JDK collection classes such as Vectors and Hashtables. You saw that using these data structures does not take full advantage of the JTree class. To fully utilize the power of the JTree class, the TreeModel and TreeNode interfaces must be understood and incorporated into the trees design. The JFC contains implementations of these interfaces, as well as the MutableTreeNode interface, that allow building of a complex tree data structure and displaying it with relative ease. An example application containing a tree component was presented. The icons used in the tree were customized to icons supplied by the application. You also saw how the JTree class allows for customization of renderers and editors. The renderer can be used to customize the display of a node for the tree.
|
![]() |
|