Listing 10.8 is a test application that works out the CustomListModel class. It creates a frame and places a list as the only component in the frame. An instance of the CustomListModel class is set as the data model for the list. The running application is shown in Figure 10.3.
Listing 10.8 The CustomListModelTest Application
package com.foley.test;
import javax.swing.*;
import com.foley.list.CustomListModel;
import com.foley.utility.ApplicationFrame;
/**
* An application that displays a JList that
* uses an instance of the CustomListModel data
* model.
*
* @author Mike Foley
**/
public class CustomListModelTest 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[] ) {
JFrame frame = new ApplicationFrame( CustomListModel Test );
CustomListModel model = new CustomListModel();
JList list = new JList( model );
list.setPrototypeCellValue( Model Element: 00000 );
JScrollPane scrollPane = new JScrollPane( list );
scrollPane.setBorder( BorderFactory.createLoweredBevelBorder() );
frame.getContentPane().add( scrollPane );
frame.pack();
frame.setVisible( true );
} // main
} // CustomListModelTest
Figure 10.3 The CustomListModel test application.
The CustomListModelTest application calls the setPrototypeCellValue method contained in the JList class. An optimization in the JList class enables the size of the list cells to be specified once and not dynamically determined for each cell. When the setPrototypeCellValue method is called, the list configures its renderer with the value passed to the method. The preferred size of the rendering component is used to set the fixed cell width and height. The fixed cell width and height can also be set directly with the setFixedCellWidth and setFixedCellHeight methods. Each of these methods has an associated get method and is a bound property of the JList class. A complete list of bound properties added by the JList class is presented in Table 10.2. The bound properties inherited from JComponent and beyond are still fired by JList instances, of course.
Table 10.2 Non-Inherited Bound Properties of the JList Class
|
Property Name
| Setter Method
| Getter Method
|
|
prototypeCellValue
| setPrototypeCellValue
| getPrototypeCellValue
|
fixedCellWidth
| setFixedCellWidth
| getFixedCellWidth
|
fixedCellHeight
| setFixedCellHeight
| getFixedCellHeight
|
cellRenderer
| setCellRenderer
| getCellRenderer
|
selectionForeground
| setSelectionForeground
| getSelectionForeground
|
selectionBackground
| setSelectionBackground
| getSelectionBackground
|
visibleRowCount
| getVisibleRowCount
| setVisibleRowCount
|
model
| setModel
| getModel
|
selectionModel
| setSelectionModel
| getSelectionModel
|
|
The setPrototypeCellValue method is a nice optimization contained in the JList class. However, it requires a great deal of information about the data model before it can choose the correct prototype value. A typical prototype value is the largest value in the model. This will ensure that every element in the list has adequate space when displayed. This information is typically not available to the list or to the client configuring the list. However, it would be reasonable for the model to know this information. A nice addition to the ListModel interface would be a getPrototypeCellValue method. This would allow the list to be configured to the correct size without guessing what that size is.
|