Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Swing JTable cells will not update as loop executes

  • 20-02-2009 5:39pm
    #1
    Registered Users, Registered Users 2 Posts: 163 ✭✭


    Hello,

    I'm haveing a bit of trouble updating a swing jTable with values as i loop through a process and update cells as i go, even though im updating the cells incrementally they don't update until the loop is finished.

    I wouldnt be bothered if the loop was fast, but the problem is that each stage in the loop takes about 2 secconds and there are about 20 increments.

    As the loop executes i update the cells in the JTabels TableModel using this code:
    jTResults.getModel().setValueAt(str[k], j, k)
    


    But the actual table deosnt get updated until the loop finishes, Any idead on how to make it update during the loop ?

    Here is all of the code:
    PipedOutputStream os = new PipedOutputStream();
                PipedInputStream is = new PipedInputStream();
                os.connect(is);
                new SendAnalysis(os,jTextField1.getText(),"50");
                ReceiveAnalysis ra = new ReceiveAnalysis(is,50);
    
                String[] str;
                InputStream ins = ra.input;
                ObjectInputStream ois = new ObjectInputStream(ins);
                for (int j = 0; j < 50; j++) {
                    str = (String[]) ois.readObject();
                    for (int k = 0; k < 4; k++) {
                        
                        System.out.println("  " + str[0] + " " + str[1] + " " + str[2] + " " + str[3]);
                        jTResults.getModel().setValueAt(str[k], j, k);
                    }
                }
    

    I've tried overriding the IDE's default DefaultTableModel for the jTable with a new one so i could call fireTableDataChanged or fireTableCellUpdated(j, k) but neither work, jTable.repaint or jTable.validate dont work either.

    S


Comments

  • Closed Accounts Posts: 324 ✭✭radioactiveman


    Did you try:

    jTResults.setModel(jTResults.getModel());
    ?
    I'm not sure how often java updates it itself.


  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    No Doesn't work unfortunately, still waits until the loop in finished, good duggestion thou,

    Seems like the TableModel doesnt get updated until the loop finishes, so it has no data for the jTable


  • Closed Accounts Posts: 174 ✭✭patftrears


    Not sure how your application works, but

    We remove table from the panel
    create new table
    add it to the panel

    or

    you could try having the tables cells as jlabels and
    get each jlabel in the cell and use .setText(kkjkl);


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    a ha!
    for (int j = 0; j < 50; j++) {
                    str = (String[]) ois.readObject();
                    for (int k = 0; k < 4; k++) {
                        
                        System.out.println("  " + str[0] + " " + str[1] + " " + str[2] + " " + str[3]);
                        jTResults.getModel().setValueAt(str[k], j, k);
                        [B]jTResults.getModel().fireTableDataChanged();[/B]
                    }
    


    ?


  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    Hi Thanks for suggestions but it still doesn't work :( i tried fireTableDataChanged before but it was no good,

    It can't be called like this: jTResults.getModel().fireTableDataChanged();
    because the getModel returns a reference to the TableModel interface which does not implement fireTab....

    I overrid the DefaultTableModel for the table then called .fireTableDataChanged() on its overriden DefaultTableModel, and like i say it didnt work,

    Maybe swing gives low priority to updating the GUI and theres nothing that can be done ?


  • Advertisement
  • Closed Accounts Posts: 324 ✭✭radioactiveman


    Can you post the class up by any chance?

    Did you try:

    TableModel myData = jTResults.getModel(); // get a reference to the model
    then later:
    myData.fireTableDataChanged();


  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    The TableModel object does not implement fireTableDataChanged, only AbstractTableModel or DefaultTableModel, i tried overrid the TableModel with a DefaultTableModel and called fireTableDataChanged on the new DefaultTableModel but that didn't work, same result, table only got updated after loop had completed. I will try this from patftrears:
    We remove table from the panel
    create new table
    add it to the panel


    I cannot post entire class, however it is simple enough,

    The class is an ide generated class with one table and only one button,

    There is only one method in the calss and it gets called by the button:
    public void button_click() {
            try {
                /*
                //make sure table is empty after the last run
                for (int j = 0; j < 50; j++) {
                for (int k = 0; k < 4; k++) {
                
                jTResults.getModel().setValueAt(null, j, k);
                }
                }
                 */
    
                PipedOutputStream os = new PipedOutputStream();
                PipedInputStream is = new PipedInputStream();
                os.connect(is);
                new SendAnalysis(os, jTextField1.getText(), "50");
                ReceiveAnalysis ra = new ReceiveAnalysis(is, 50);
    
                String[] str;
                InputStream ins = ra.input;
                ObjectInputStream ois = new ObjectInputStream(ins);
                for (int j = 0; j < 50; j++) {
                    str = (String[]) ois.readObject();
                    for (int k = 0; k < 4; k++) {
    
                        System.out.println("  " + str[0] + " " + str[1] + " " + str[2] + " " + str[3]);
                        jTResults.getModel().setValueAt(str[k], j, k);
                        jTResults.setModel(jTResults.getModel());
    
                    }
                }
                ins.close();
    
    
            } catch (Exception e) {
            }
        }
    


  • Closed Accounts Posts: 324 ✭✭radioactiveman


    I have no idea about this actually.
    It could be that the data is not being updated in the event thread, but I did a test to see and doing this (with SwingUtilities.invokeLater()) made no difference.
    If anyone can get this I would be very interested in seeing it.

    The table will not update without fireTableDataChanged() but getting it to update just after each row is added is a different thing. Maybe this is a limitation in Swing?
    I tried repainting the table each time but no go.
    I might post this up on JavaRanch to see if they have any idea...


  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    I have no idea either, seems weird, but i think swing may do things in chunks for performance, i got the invokeLater() suggestion from the sun forums also, but it doesnt make a difference if the update is called there


  • Registered Users, Registered Users 2 Posts: 5,112 ✭✭✭Blowfish


    I've had similar issues previously. Seems repaint() etc. just tell Swing to update when it can, rather than immediately.

    This has worked for me as a workaround:
    jTResults.paintComponents(jTresults.getGraphics());
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 163 ✭✭stephenlane80


    This didn't work for me :(


Advertisement