I have been trying to put together an app that includes a JTable.
I had been using a Vector to hold the values and then passed two
vectors to the JTable constructor. One Vector holds the data and the
other holds the column headers.
I then began reading about how the TableModel is a better way to
organize the data for use with a JTable. Always one to try something
new, I started experimenting with the DefaultTableModel object.
Then I came across the DefaultTableColumnModel object and the
TableColumn object. Soon, I saw that there was a JTableHeader
component as well.
Before long I was thoroughly confused as to what is the 'best' way of
organizing the data for display. Vectors? TableModels?
TableColumnModels?
On top of that, I am really getting wrapped around the axle trying to
figure out how TableModels and TableColumnModels are supposed to be
used.
Are TableModels used instead of TableColumnModels or as a complement
to? Is there an advantage to using these over a Vector?
Does anyone have some insight into these various objects? Maybe a link
to a tutorial that covers the assorted table models in depth. I know
Sun has the tutorials for JTable et.al., but I haven't found anything
for the TableModel stuff.
When would I need to use JTableHeader?
My list of questions is growing everytime I look at it, but I will stop
there.
Any help would be appreciated.
Thanks.
> Before long I was thoroughly confused as to what is the 'best'
> way of organizing the data for display. Vectors? TableModels?
> TableColumnModels?
Actually, anytime you use a JTable, you will be using a TableModel, a
TableColumnModel, some TableColumns, and probably a JTableHeader. You may
explicitly provide these objects, but if you do not, the JTable will
create them for you. For example, when you construct a JTable with two
Vectors, it creates a DefaultTableModel with those same two Vectors.
> On top of that, I am really getting wrapped around the axle trying to
> figure out how TableModels and TableColumnModels are supposed to be
> used.
A JTable is a visual component for displaying data in a tabular form. It
knows *how* to display the data, but it does not know anything about how
the data is stored.
A TableModel provides access to the data that the JTable displays. How it
does that is up to the TableModel itself. It can retrieve the data from a
Vector (as you have seen), a database, or in any other structure that it
deems appropriate. It just needs to be able to provide access to the data
as defined by the TableModel interface. Take a look at that interface to
see exactly what a TableModel implementation needs to do.
The TableColumnModel and TableColumns maintain information about the
columns in the table. In most cases, you can allow the JTable to create
these for you using information retrieved from the getColumnName() and
getColumnClass() methods of the TableModel. In some cases, you will want
to set properties on the TableColumns. You can retrieve the constructed
TableColumns from the TableColumnModel, and you can get that from the
JTable.
> Does anyone have some insight into these various objects? Maybe a link
> to a tutorial that covers the assorted table models in depth. I know
> Sun has the tutorials for JTable et.al., but I haven't found anything
> for the TableModel stuff.
The Java Tutorial does cover TableModel, TableColumnModel, etc. For more
in depth coverage, I would recommend Kim Topley's JFC book - part of the
Core Java series.

Signature
Regards,
John McGrath