Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTables and java  RSS feed

John Gregory
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an app that has a jfilechooser. From there a user selects a file.
Once they click Open, I want the contents to display in a jtable. For
this,I'm using vectors for the column names and the date. The thing is
the data vector won't be populated until the user selects a file. I'm
using a defaulttablemodel, but am not married to it...I think i could
use abstracttablemodel, as well. Any opinions on which I should use?

Now, I have some pre-processing to perform on my data, as I don't know
what format in which it arrives, binary, text...whatever (that, I have solved). What I can't figure out is how to update the data vector with
my data and display it in the table.

Next, if a user selects a particular row, I populate another JTable with
some further information. I've never used JTables and I can't find any
examples for what I'm trying to do.

If it matters, I'm using jdk5 and netbeans IDE.

Any ideas would be greatly appreciated!


Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!