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.
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
}
});
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With