Java Reference
In-Depth Information
16.7.2.7
Look again at our GUI application. In its center you see the table object:
JTable : The Workhorse of Data Display
This is a JTable . A simple way to create a JTable is by passing in two
arrays to the constructor—first, a two-dimensional array of data objects, and
second, a one-dimensional array of column names. Notice that we said data
objects; you need to use Integer objects, not simple int types, and Double s
instead of double s. This allows the constructor to take any Object type and
display it in the table via the object's toString() method.
While this form of a table is simple to use, it usually isn't enough for all
the various things you'll want to do with a table. Let's look at the “industrial
strength” table initialization. For that, we need to talk about a table model.
16.7.2.8
If you've ever taken an object-oriented design class, they've probably talked
about the Model/View/Controller design pattern. (If you haven't taken such a
class, at least read a good book or two on the subject; it will improve your Java
programming skills.) A simpler version of this pattern is the View/Model pat-
tern. What it describes is separating the core of the data from the frill of its
presentation— what you want to display versus how you want to display it. The
Model is the underlying data; the View is one particular way to show that data.
This View versus Model distinction is used to great effect with JTable
and TableModel objects in Swing. What you need to do is create a
TableModel , then give that TableModel to the JTable via the JTable 's
constructor. The TableModel will give you all sorts of control over your
data—how, where, and when to get or update it. The JTable will display it
and let you rearrange or resize the columns.
Rather than implement a complete TableModel from scratch, Swing gives
us a helping hand with its AbstractTableModel class. AbstractTableModel
is a partially implemented class which handles most of the grundy details—it
Table Model
Search WWH ::




Custom Search