
JTable and TableModel
javax.swing.JTable is another powerful Swing component for displaying com-
plex data structures. Like
JTree, JTable relies on a separate model object to hold
and repr esent the data it displays and has its own package of helper classes,
javax.swing.table. This package contains the TableModel inter face and its default
implementations,
AbstractTableModel and DefaultTableModel.
If your table data is tidily organized, it is easy to use
JTable without worrying
about the
TableModel.Ifyour data is an array of rows, where each row is an array
of objects, you can just pass this
Object[][] dir ectly to the JTable constructor. If
you want, you can also specify an optional array of column names. This is all you
need to do: the
JTable does the rest. This technique also works if your data is
stor ed in a
Vector of rows, where each row is itself a Vector.
Often, however, your data is not as regular as that. When you want to display a
tabular view of data that is not, by nature, tabular, you must implement the
Table-
Model inter face (or, mor e likely, subclass the AbstractTableModel class). The job
of this
TableModel implementation is to serve as the interface between your data,
which is not neatly organized into a table, and the
JTable object, which wants to
display a table. In other words, your
TableModel pr esents a neat tabular view of
your data, regardless of how the data is organized underneath. ...