File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Displaying data in a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Displaying data in a table" Watch "Displaying data in a table" New topic

Displaying data in a table

Donald King

Joined: Dec 08, 2000
Posts: 16
I am writing a simple application that retrieves data from an Access database and displays it in a table (JTable). I need to be able to repopulate the table with new rows from the database, based on user filtering requests.
I'm using the DefaultTableModel.setDataVector methods to do this, although I'm not sure this is the right thing to do. The Java documentation says:
"Because the JTable is now much easier to set up with custom models the DefaultTableModel is less useful than it was in previous releases. Instead of copying the data in an application into the DefaultTableModel, we recommend wrapping it in the methods of the TableModel interface and passing the real data to the JTable as above."
Unfortunately, there is no description on how to do this using the TableModel interface. In particular, there are no methods defined in TableModel for loading in vectors of data, just column at a time, which seems tedious. If I follow the documentations recommendation, how do I get data into the table?
- Don
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
This is a nice tutorial on tables:
This is the code for a simple table using a TableModel:

"JavaRanch, where the deer and the Certified play" - David O'Meara
Donald King

Joined: Dec 08, 2000
Posts: 16
Thanks. The sample code you attached was almost identical to what I started with. But I ran into problems with the update. My application is retrieving rows using a default query from a database table and displaying them in the JTable. If the user chooses to change the query, I am going back to the db and populating the table with a new set of rows.
In order to have the new rows displayed, I need to update the JTable's TableModel. Using DefaultTableModel, I can reset the data through the setDataVector method. This operation causes the DefaultTableModel object to fire a message to the JTable to repaint itself. In the sample code you attached, if I change the data array, it won't repaint in the table automatically. How do I get this new data reflected in the table?
- Don
Paul Bull
Ranch Hand

Joined: Feb 14, 2001
Posts: 37
Hey Donald,
I was working on a similar program recently and the following is what I did to get the data from the ResultSet into the JTable and
get it to repaint itself. I got this information from Ivor Horton's "Beginning Java2" (Wrox publishing) Ch.18.
A great book for beginning Java programmers.
The following code was put into a seperate class as an extension of AbstractTableModel. The purpose of this class is to implement a method (setResultSet) which takes a row from the ResultSet places it into a String array. The array is then placed into a vector. This is done in a while loop using the .next() method of
ResultSet until all the rows of the ResultSet are placed into the vector. Details are as follows:

After this class is created another class is created to implement
the GUI to hold the JTable. The above mentioned class (being an extension of AbstractTableModel) is put into the JTable, thus the vector of query rows is loaded into the JTable.

(edited by Cindy to format code)

[This message has been edited by Cindy Glass (edited February 22, 2001).]
I agree. Here's the link:
subject: Displaying data in a table
jQuery in Action, 3rd edition