JTable is a component that displays rows and columns of data. You can drag the cursor
on column boundaries to resize columns. You can also drag a column to a new position.
Depending on its configuration, it is also possible to select a row, column, or cell within the
table, and to change the data within a cell. JTable is a sophisticated component that offers
many more options and features than can be discussed here. (It is perhaps Swing's most
complicated component.) However, in its default configuration, JTable still offers
substantial functionality that is easy to use--especially if you simply want to use the
table to present data in a tabular format. The brief overview presented here will give
you a general understanding of this powerful component.
Like JTree, JTable has many classes and interfaces associated with it. These are
packaged in javax.swing.table.
At its core, JTable is conceptually simple. It is a component that consists of one or more
columns of information. At the top of each column is a heading. In addition to describing
the data in a column, the heading also provides the mechanism by which the user can
change the size of a column or change the location of a column within the table. JTable does
not provide any scrolling capabilities of its own. Instead, you will normally wrap a JTable
inside a JScrollPane.
JTable supplies several constructors. The one used here is
JTable(Object data[ ][ ], Object colHeads[ ])
Here, data is a two-dimensional array of the information to be presented, and colHeads is a
one-dimensional array with the column headings.
JTable relies on three models. The first is the table model, which is defined by the
TableModel interface. This model defines those things related to displaying data in a
two-dimensional format. The second is the table column model, which is represented by
TableColumnModel. JTable is defined in terms of columns, and it is TableColumnModel that
specifies the characteristics of a column. These two models are packaged in javax.swing.table.
The third model determines how items are selected, and it is specified by the
ListSelectionModel, which was described when JList was discussed.
A JTable can generate several different events. The two most fundamental to a table's
operation are ListSelectionEvent and TableModelEvent. A ListSelectionEvent is generated
when the user selects something in the table. By default, JTable allows you to select one or
more complete rows, but you can change this behavior to allow one or more columns, or
one or more individual cells to be selected. A TableModelEvent is fired when that table's
data changes in some way. Handling these events requires a bit more work than it does to
handle the events generated by the previously described components and is beyond the
scope of this topic. However, if you simply want to use JTable to display data (as the
following example does), then you don't need to handle any events.
Here are the steps required to set up a simple JTable that can be used to display data:
1. Create an instance of JTable.
2. Create a JScrollPane object, specifying the table as the object to scroll.
3. Add the table to the scroll pane.
4. Add the scroll pane to the content pane.
Search WWH :