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


Table Selection Models

The SimpleTableTest application was presented in the previous section. This application created a JTable instance that used the default table selection model row selection. However, the JTable class supports many selection modes. The table delegates both column and row selection to a class that implements the ListSelectionModel interface. Internally, this class is the DefaultListSelectionModel class that was presented in Chapter 10, “JList, JComboBox, and Bound Controls.” Thus, the selection modes for rows and columns in a table are the same as rows in a list. These are single selection mode, contiguous selection mode, and non-contiguous selection mode. The table can also be configured to allow only cells to be selected instead of entire rows or columns.

The selection mode can be set by querying the selection model from the table and setting the desired mode directly, or by calling convenience methods provided by the JTable class that update the selection models themselves.

You’ll start by looking at the convenience functions provided by the JTable class. The easiest methods to use for configuring selection are setRowSelectionAllowed and setColumnSelectionAllowed. Passing true to either of these methods enables the selection, and false disables it. Passing false to both these methods will enable cell selection. The setSelectionMode method is used to set the type of selection allowed. The selection mode is defined in the ListSelectionModel interface described in Chapter 10. Possible modes that can be passed to this method are ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.SINGLE_INTERVAL_ SELECTION, and ListSelectionModel.MULTIPLE_INTERVAL_SELECTION. For a complete description of these modes, see Chapter 10.

Calling the setSelectionMode method contained in the JTable class sets the new mode for both the row and column selection model. If you require a different selection mode for rows and columns, the mode must be set on the selection model itself. The row selection model can be obtained by calling the getSelectionModel method contained in the JTable class. To obtain a handle to the column selection model, you must go through the column model. The column model is obtained via the getColumnModel method. With the column model in hand, its getSelectionModel method can be called to obtain the column selection model. Then the setSelectionMode method of the model can be called.

The foreground and background colors of selected cells can be specified by using the setSelectionForeground and setSelectionBackground methods, respectfully. There are corresponding “get” versions of these methods to obtain the current value of the property. The selectionForeground and selectionBackground properties are the only bound properties contained in the JTable class. At first glance this may seem odd for such a large class. However, as you will see later in this chapter, the JTable class delegates much of its functionality to support classes that contain bound properties or defines dedicated listener interfaces and events to notify observers of changes.

With that said, there are properties in the JTable class that should be bound but are not. The gridColor property immediately comes to mind. The bound properties for the JTable class are shown in Table 12.1. Notice that the property names are hard-coded strings in the JTable class. Use caution when testing for these property names in your source code.

Table 12.1 Bound Properties of the JTable Class

Property Name Setter Method Getter Method

selectionBackground setSelection getselection
Background Background
selectionForeground setSelection getselection
Foreground Foreground


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.