Results 1 to 9 of 9
Thread: Update JTable data
- 03-30-2010, 02:36 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Update JTable data
I'm trying to figure out how I might be able to update my JTable data using a Timer. Though the TimerTask will run just fine with an empty run() method, I cannot modify the String[][] array being used by the TableModel since the Timer is an inner class and can only access external variables if they've been declared as final. Here's an example of what I'm doing...
Java Code:String rows[][] = //table data String cols[] = //columns java.util.Timer clock = new java.util.Timer(); clock.schedule(new TimerTask() { public void run() { rows = //modify table data, not possible from here } }, 0, 30000);
-
This is Swing, so don't use a java.util.Timer, but rather a javax.swing.Timer or "Swing Timer". Also, if possible, use a DefaultTableModel for your JTable's model. They're a lot easier to deal with then AbstractTableModels.
For more help, your best bet is to create and post an SSCCE. Please see the link in my signature below for more details.
Much luck!Last edited by Fubarable; 03-30-2010 at 02:41 AM.
- 03-30-2010, 03:08 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
No and you should never try to do so. To udpate the data in the table you update the TableModel directly. That is you would use the setValueAt(...) method. The DefaultTableModel implements this method for you and implements other methods like addRow(...) and insertRow(...) to make dynamic modification easier.I cannot modify the String[][] array being used by the TableModel
Then declare them as final.the Timer is an inner class and can only access external variables if they've been declared as final.
- 03-30-2010, 03:29 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
Camickr: The TableModel is created using data that is read from a file. Since this file can be modified at any time, I would like the program to re-read it and update the table model at regular intervals. Would it still be feasible to use methods such as addRow() for this?
Fubarable: I'm using a swing timer now, but I'm back at square one since it still uses an inner class ActionListener to actually carry out the instructions. This is a problem because the variable that the timer needs to update is declared outside of the inner class as it's needed in the rest of the main method. Here's an SSCCE...
Java Code:public class guiMain { public static void main(String args[]) { //create main gui components JFrame frame = new JFrame("frame"); //table data final fileReader datafile = new fileReader(); String rows[][] = datafile.getData(); //returns data array String cols[] = {"one","two","three"}; ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { rows = datafile.getData(); //reload table data, cannot access external variable } }; new javax.swing.Timer(1000, taskPerformer).start(); //table TableModel model = new DefaultTableModel(rows, cols) { public Class getColumnClass(int column) { Class returnValue; if ((column >= 0) && (column < getColumnCount())) { returnValue = getValueAt(0, column).getClass(); } else { returnValue = Object.class; } return returnValue; } }; final JTable table = new JTable(model); //bind components frame.add(table); frame.setSize(600, 600); frame.setVisible(true); } }Last edited by DC200; 03-30-2010 at 03:31 AM.
-
It's not the JTable that has to be final, and you'll not need to access the JTable from within the inner class, but it's the DefaultTableModel that needs to be accessable as you'll be probably calling its getValueAt and setValueAt methods.
Oh, and you'll want to declare your DefaultTableModel variable of type DefaultTableModel, not TableModel and you'll need to do this above your ActionListener code so it's accessable.Last edited by Fubarable; 03-30-2010 at 03:38 AM.
- 03-30-2010, 12:20 PM #6
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
I'd rather not use the setValueAt and getValueAt methods as the table data is stored inside a file. What I'm trying to do is re-read the file, store it into the same String[][], and then have it create the DefaultTableModel again using that data.
Should I hold the contents of the newly read file in a private String[][] within the ActionListener and then use that to do setValueAt for each for in the JTable?Last edited by DC200; 03-30-2010 at 12:29 PM.
- 03-30-2010, 04:31 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
So you create new arrays in the method that reads the file. There is no need to reference the original arrays.
-
AFAIK, yours is a workable plan. But make sure to declare the final JTable above the ActionListener.
I like your idea better -- create a new DefaultTableModel from within the ActionListener and set the table's model.Should I hold the contents of the newly read file in a private String[][] within the ActionListener and then use that to do setValueAt for each for in the JTable?
Let's see what Rob or the other Swing gurus have to say though.
Edit: never mind, Rob was posting at the same time!Last edited by Fubarable; 03-30-2010 at 04:39 PM.
- 03-30-2010, 10:10 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 49
- Rep Power
- 0
I've implemented it a little differently.
The DefaultTableModel is declared as final outside of the ActionListener with fixed columns and no rows. The Timer then reads the file during program startup, stores the data into an array and then uses that to insert data into the table. From there on, it will repeat this task at regular intervals to ensure that the table's data is up to date.
Though this method seems to be working fine at the moment, I'm hoping that it's as efficient as it is effective.Last edited by DC200; 03-30-2010 at 10:22 PM.
Similar Threads
-
update a change in a Jtable directly to the mysql table
By Muffel in forum JDBCReplies: 0Last Post: 02-21-2010, 11:51 AM -
Update the GUI in JTable
By itaipee in forum AWT / SwingReplies: 3Last Post: 04-12-2009, 12:28 PM -
How to update data for a JPA many-to-many relationship?
By abhijit.sarkar in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 11-04-2008, 08:48 AM -
how to refresh data of the JTable
By paty in forum JDBCReplies: 3Last Post: 08-17-2008, 12:01 PM -
[SOLVED] Update jTable with new data (especially Date value Format)
By gustio in forum New To JavaReplies: 2Last Post: 08-12-2008, 12:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks