How to immediately change jLabel text inside a MouseEvent handler?

I have a problem with updatable swing components inside the mouse action event handler. The problem is that all jLabels that are changed in this function, that their changes are visible after jButton1MouseClicked () is executed. Here is my function:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      

    int cycles = Integer.parseInt(c.settings.get("cycles"));
    statusMessageLabel.setText("Data collection in progress...");

    for(int i=1;i <= Integer.parseInt(c.settings.get("cycles"));i++) {

        jLabelCycle.setText(Integer.toString(i));

        //here are some functions which are implementing data collection


        if(i < cycles){
            int counter = Integer.parseInt(c.settings.get("interval"));
            while(counter >= 0){
                jLabelTimer.setText(Integer.toString(counter));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(AppCView.class.getName()).log(Level.SEVERE, null, ex);
                }
                counter--;
            }
        }

    }
    statusMessageLabel.setText("Data collection has been finished.");
}    

Could you help me? I really need these changes to be visible, because one jLabel renders the count until the next loop, and the second renders the actual loop number. I tried to rename the call () function in all jLabels after I changed them, but that didn't help.

Thanks to everyone for any help.

+3
source share
1 answer

AWT, GUI-, , repaints, - Thread Dispatch Thread (EDT) AWT. , .

javax.swing.Timer Thread.sleep.

, . , AWT Swing, AWT EDT java.awt.EventQueue.invokeLater.

+6

All Articles