Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing JTable update

Tags:

java

swing

jtable

Am working on a project in swings and in that am adding rows to a JTable in a while loop.

My Scenario is this :-

As soon as the user clicks a button the program enters a while() loop and start adding rows to the DefaultTableModel of the Jtable one by one till the while loop terminates. But the thing is that the table gets updated with the data only after the while loop has ended.I want it to update after adding each row and show it on the UI.

It would be really nice if someone could help me out with this provide a solution

I have already tried repaint() after adding each row but it didn't work.

like image 627
Jinith Avatar asked Nov 11 '10 06:11

Jinith


2 Answers

You need to run your operation in a seperate thread and then update the JTable in the gui thread. Something like this:

public void someButtonClicked(params...) {
    new Thread(new Runnable() {
        public void run() {
            longOperation();
        }
    }).start();
}

public void longOperation() {
    for(int i=0; i<1000; i++) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // add new row to jtable
            }
        });
    }
}
like image 81
Gerco Dries Avatar answered Oct 03 '22 05:10

Gerco Dries


i think you should go for frequently updating the row. There is a tutorial given by sun called the "Christmas tree". Here is link for that

http://java.sun.com/products/jfc/tsc/articles/ChristmasTree/

Above link will help you for frequently update rows in jTable.

like image 38
Toman Avatar answered Oct 03 '22 06:10

Toman