• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTables and java

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

Thanks,

John
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic