Java Reference
In-Depth Information
Table 18-1.
JTable Properties (Continued)
Property Name
Data Type
Access
selectionModel
ListSelectionModel
Read-write bound
showGrid
boolean
Write-only
showHorizontalLines
boolean
Read-write bound
showVerticalLines
boolean
Read-write bound
surrendersFocusOnKeystroke
boolean
Read-write
tableHeader
JTableHeader
Read-write bound
UI
TableUI
Read-write
UIClassID
String
Read-only
■
Note
Row heights are not fixed. You can change the height of an individual row with
public void
setRowHeight(int row, int rowHeight)
.
Most of the
JTable
properties fit into one of three categories: display settings, selection
settings, and auto-resizing settings.
Display Settings
The first subset of properties in Table 18-1 allows you to set various display options of the
JTable
.
In addition to the inherited
foreground
and
background
properties from
Component
, you can change
the selection foreground (
selectionForeground
) and background (
selectionBackground
) colors.
You also control which (if any) gridlines appear (
showGrid
), as well as their color (
gridColor
).
The
intercellSpacing
property setting deals with the extra space within table cells.
Selection Modes
You can use any one of three different types of selection modes for a
JTable
. You can select
table elements one row at a time, one column at a time, or one cell at a time. These three settings
are controlled by the
rowSelectionAllowed
,
columnSelectionAllowed
, and
cellSelectionEnabled
properties. Initially, only the row selection mode is allowed. Because the default
ListSelectionModel
is in multiselect mode, you can select multiple rows at a time. If you don't
like multiselect mode, you can change the
selectionMode
property of the
JTable
, causing the
selection mode of the rows and columns of the
JTable
to change accordingly. Cell selection is
enabled when both row and column selections are enabled.
If you're interested in whether any of the rows or columns of the
JTable
are selected,
you can inquire with one of the six additional properties of
JTable
:
selectedColumnCount
,
selectedColumn
,
selectedColumns
,
selectedRowCount
,
selectedRow
, and
selectedRows
.
The
ListSelectionModel
class provides constants for the different selection modes. The
ListSelectionModel
interface and
DefaultListSelectionModel
class were both covered with