I have a jTable and a jButton. When clicked, the button’s actionPerformed method calls another method that contains a while loop and adds a row to the table’s model (DefaultTableModel) each time though the loop. The while loop can run for a few minutes so I want it to show in the GUI the rows being added to the table each time, one by one. However right now it adds all the rows to the table together after the loop is finished, so instead of incrementing the number of rows one by one over the course of a few minutes it goes from a few minutes of showing a table with 0 rows to then instantly having thousands. I’ve tried calling updateUI, repaint etc on the table as well as calling fireTabledDataChanged etc on the model but none of this made any difference. I’ve also tried using a Swing Timer but to no avail. I’d appreciate any help or guidance offered, thanks.
I have a jTable and a jButton. When clicked, the button’s actionPerformed method calls
Share
Read the section from the Swing tutorial on Concurrency which explains in more detail about how the EDT works. In addition to the create your own Thread and use SwingUtilties.invokeLater(), you can also use the newer of approach of using a SwingWorker. The tutorial contains an example.
Never do something like that. Even if it fixes your problem, it is the wrong solution. You are not updating the UI, you are updating a component.