Swing JTable Update

I am working on a swing project and adding lines to JTable in a while loop.

My scenario is this: -

As soon as the user clicks the button, the program enters a while () loop and starts adding rows to the DefaultTableModel Jtable one by one until the while loop completes. But the fact is that the table is updated with data only after the while loop has ended. I want it to be updated after adding each line and showing it in the user interface.

It would be very nice if someone could help me provide a solution to this.

I already tried redrawing () after adding each line, but that didn't work.

+5
source share
3 answers

, JTable gui. - :

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
            }
        });
    }
}
+7

while Swing , , ( Performed ).

, - , ? Swing Timer class (javax.swing.Timer) , . , Swing . Swing Threading, . - , , . " ".

Re: calling repaint () - this will not work like repaint (), and other methods, such as validate (), etc., will only mark the component that needs to be repainted - the component will not actually be redrawn on the screen until Swing gets a chance to do this, and if you catch the Swing thread in a while loop, it will not be free to take a picture until you finish the loop and the event handling code ends.

0
source

All Articles