This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Dec 08, 2000
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
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).]