Results 1 to 9 of 9
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default 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);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by DC200 View Post
    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...
    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.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I cannot modify the String[][] array being used by the TableModel
    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.

    the Timer is an inner class and can only access external variables if they've been declared as final.
    Then declare them as final.

  4. #4
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    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.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    So you create new arrays in the method that reads the file. There is no need to reference the original arrays.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by DC200 View Post
    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.
    AFAIK, yours is a workable plan. But make sure to declare the final JTable above the ActionListener.

    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?
    I like your idea better -- create a new DefaultTableModel from within the ActionListener and set the table's model.

    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.

  9. #9
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Default

    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

  1. Replies: 0
    Last Post: 02-21-2010, 11:51 AM
  2. Update the GUI in JTable
    By itaipee in forum AWT / Swing
    Replies: 3
    Last Post: 04-12-2009, 12:28 PM
  3. How to update data for a JPA many-to-many relationship?
    By abhijit.sarkar in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 11-04-2008, 08:48 AM
  4. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM
  5. Replies: 2
    Last Post: 08-12-2008, 12:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •