Welcome to JavaRanch, John
Precisely this week I have gone trough a lot of suffering with JTable, since I am working on my
SCJD certification. Phew!
Look, I think that you can obtain a lot of flexibility using your own
TableModel if your data requires special handling and presentation. I believe the
DefaultTableModel is for the presentation of simple information, but when you want to handle lots of value objects and different data types and differnt ways of presentation, then ther is a lot of customization to be done.
If you create you own
TableModel, whenever you change your data source within your
TableModel simply invoke the
setDataChanged() on your
TableModel and your
JTable will be updated accordingly. It has other methods like this for structural changes, addition and change of data.
One of the greatest advantages of creating your own
TableModel is that you can add new functionality. For instance, yesterday I created a
TableModel that knows how to sort columns, and instead of displaying a vector I display a
List<Hotel> (a list of hotels).
To display text in different formats consider creating
CellRenders. A
CellRenders is component that the table uses to display the model. For instance, you can create a
CellRenders to display dates in particular format.
If you ever changed the
TableModel of your
JTable as far as I have learnt, you would have to redefine the
CellRenderers. Therefore, its better just to change the source of the
TableModel than the
TableModel itself and then call the
setDataChanged() method.
As for your selection and changes
you should consider the use of a
TableModelListener and
ListSelectionListener.
Finally, for examples go to
Java Almanac
[ October 17, 2006: Message edited by: Edwin Dalorzo ]