Read an Excerpt
Chapter 17: Tables
Chapter 16 took an in-depth look at the Swing JTree component. In this Chapter,
we'll examine the many details of the JTable component. The component is the
standard Swing component for displaying two-dimensional data in the form of a
grid, as shown in Figure 17-1.
NOTE All the examples in this Chapter will work fine without configuring
your environment to display Japanese fonts. However, instead of seeing
the ideographs, you will see characters such as question marks or boxes,
depending upon your platform. In order to see the Kanji ideographs in the
sample programs, you will need to be using the Japanese version of the
font.properties file (properties.ja ) and have the necessary Japanese
fonts installed. In addition to the Windows NT installation CD, you can
find the necessary Windows fonts at http://ftp.monash.edu.au/pub/nihongo/
ie3lpkja.exe. Solaris users must contact Sun to request the Asian outline
fonts for Solaris environments. More on adding fonts to the Java runtime
environment is at http://java.sun.com/products/jdk/1.3/docs/guide/intl/
fontprop.html.
Figure 17-1: Sample JTable...
Introducing Tables
Like the JTree component, the JTable component relies on numerous support
classes for its inner workings. For the JTable , the support classes are found in the
javax.swing.table package. The cells within the JTable can be selected by row,
column, row and column, or individual cell. It's the responsibility of the current
ListSelectionModel settings to control the selection within a table.
The display of the different cells within a table is the responsibility of the
TableCellRenderer; the DefaultCellRenderer offers one such implementation of
the TableCellRenderer interface in a JLabel subclass.
Managing the data stored in the cells is accomplished through an implementation
of the TableModel interface. The AbstractTableModel provides the
basics of an implementation of the interface without any data storage. By comparison,
the DefaultTableModel encapsulates the TableModel interface and uses a
vector of vectors for the data storage. You extend AbstractTableModel if you need
a different type of storage than the kind supplied by the DefaultTableModel, for
instance, if you already had the data in your own data structure.
The TableColumnModel interface and the DefaultTableColumnModel implementation
of the interface manage the table's data as a series of columns. They work
together with the TableColumn class to allow for greater flexibility in manipulating
individual columns. For example, you can store columns of data in the
TableModel in an order that's different than the display order within the JTable. The TableColumnModel manages a second ListSelectionModel to control table column
selection.
At the top of every column is a column header. By default, the TableColumn
class relies on the JTableHeader class to render a text column header. Nevertheless,
you must embed the JTable in a scroll pane to see the default header.
Cells within a JTable can be editable. If a cell is editable, how the editing
works depends on the TableCellEditor implementation, such as the
DefaultCellEditor implementation, which extends from AbstractCellEditor . In
addition, no classes exist to handle individual rows. Rows must be manipulated
on a cell-by-cell basis. However, behind the scenes, the JTable does use the
SizeSequence utility class to deal with variable height rows. However, you won't
need to manipulate it yourself.
Figure 17-2 shows these class interrelationships.
NOTE There are additional interrelationships among the elements used
by the JTable component. These relationships will be explored later in this
chapter with each specific interface and class.
To visualize how the JTable elements all fit together, examine Figure 17-3.
Class JTable
We'll first look at the JTable class, which gives you a way to display data in
tabular form....
Read More