![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
A Simple ListCellRendererThe SimpleListCellRenderer class is shown in Listing 10.2. This class is adapted from the default ListCellRenderer used by the JList class, but fixes a couple of bugs contained in the default renderer. The SimpleListCellRenderer class extends the JLabel class. In its constructor, the component is configured to be opaque, forcing the background color to be painted by the renderer. The components border is created in the constructor, and a reference is kept to the border instance. The getListCellRendererComponent configures the JLabel to the current state of the given value. If the list item is selected, the selected colors are queried from the list and set on the label. If not, the normal colors are obtained from the list and set on the label. Similarly, the lists font is queried and set on the label. If the list item is an instance of an icon, it is added to the label and the text is cleared. Otherwise, the toString method of the list item is called to get the objects string representation. Finally, the Border property is set for the label and the label is returned to the caller. The getListCellRendererComponent method of a renderer has the potential to be called many times. It also has a significant impact on a lists performance. This being the case, the method must be efficient. Avoid unnecessary object creation in this method. Whenever possible, you should reconfigure objects for the current value rather than creating a new instance. Listing 10.2 The SimpleListCellRenderer Class package com.foley.utility; import java.awt.*; import java.io.*; import javax.swing.*; import javax.swing.border.*; public class SimpleListCellRenderer extends JLabel implements ListCellRenderer, Serializable { protected static Border emptyBorder; public SimpleListCellRenderer() { super(); emptyBorder = BorderFactory.createEmptyBorder( 2, 2, 2, 2 ); // // We change the background color, so need to be opaque. // setOpaque(true); } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if( isSelected ) { // // Draw like any other list when selected. // setBackground( list.getSelectionBackground() ); setForeground( list.getSelectionForeground() ); } else { // // Paint in the list normal foreground and background colors. // setBackground( list.getBackground() ); setForeground( list.getForeground() ); } // else setFont( list.getFont() ); if( value instanceof Icon ) { setIcon( ( Icon )value); setText( ); } else { setIcon( null ); setText( (value == null ) ? : value.toString() ); } // else setBorder( ( cellHasFocus ) ? UIManager.getBorder( List.focusCellHighlightBorder ) : emptyBorder ); return this; } } // SimpleListRenderer The SimpleListRenderer, as well as more exciting list renderers, are demonstrated in the next section covering the JList class. Using the JList ClassThe JList class provides the visual representation for selecting items in a set. In its simplest form, a list can be instantiated with an array or vector of objects. The list will create one item in the list for each element in the vector or array. This technique is demonstrated in the ListTest application for an array in Listing 10.3, which results in the list shown in Figure 10.1. Similarly, the setListData method can be called to replace the data contained in the list to the given array or vector. Listing 10.3 The ListTest Application package com.foley.test; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import com.foley.utility.ApplicationFrame; /** * An application that displays JList instances * in its frame. * * @author Mike Foley **/ public class ListTest extends Object { /** * Application entry point. * Create a frame, the list and display it. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { String[] data = {Apple, Orange, Grape, Banana }; JFrame frame = new ApplicationFrame( List Test ); JList list = new JList( data ); frame.getContentPane().add( list, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); } // main } // ListTest
The list in the previous example allows multiple items to be selected in the list. In Figure 10.1, all items are selected except the item Grape. The default look-and-feel behavior is to perform a Ctrl+mouse-click to select items after the first item is selected. The list can be configured to allow the selection of a single item or multiple items. When multiple item selection is allowed, the selection can be specified to allow a continuous range or arbitrary items in the list. The setSelectionMode method can configure the list with the desired selection model. The constants shown in Table 10.1 are defined in the JList class for the acceptable selection modes. The getSelectionMode method returns the current selection mode of the list.
|
![]() |
|