0

I made a program that opens a database and makes an update, how this update is very long, I've tried to make a new frame where the user could see the state of the update.

The problem is: When I launch the new Thread the structure of the window is created but doesn't show anything else. When the update has finished, then, the window finished to load completely all the content.

What am I doing wrong ?

public class finestra extends Thread{
    @Override
    public void run(){
        label1.setText(getMissatge1());
        label1.setHorizontalAlignment(JLabel.CENTER);
        label2.setText(getMissatge2());
        label2.setHorizontalAlignment(JLabel.CENTER);
        label3.setHorizontalAlignment(JLabel.RIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new GridLayout(0,1));
        frame.setSize(300, 100);
        frame.add(label1);
        frame.add(label2);
        frame.add(label3);
        //frame.pack();
        frame.setVisible(true);
        int temps = 0;
        boolean ok = false;
        while(ok == false){
            temps++;
            try{
                Thread.sleep(1000);
                label1.setText(getMissatge1());
                label2.setText(getMissatge2());
                label3.setText("Working " + String.valueOf(temps));
            }catch (Exception a){

            }
        }
    }
}

And this is the way that i launch the thread:

finestra Finestra = new finestra(); Finestra.start();

Thanks a lot!

Jaume Ill
  • 1
  • 1

1 Answers1

0

You're attempting to make gui updates outside of the event dispatch thread. This is not recommended and will cause the behaviour you're seeing...

see How to update java GUI from Thread? for techniques about how to ensure the updates hapeen in the event dispatch thread.

Community
  • 1
  • 1
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101