tags:

views:

386

answers:

4

In my JFrame it loops to do some task, I want to see the status in the JFrame title, so I have something like this :

frame.setTitle("Current status [ "+Auto_Count_Id+"/"+Auto_Count_Total+" ]");

But it's not repainting as I need it to. So I tried the following :

<1>

    SwingUtilities.invokeLater(new Runnable() 
    {
      public void run()
      {
        frame.setTitle("Current status [ "+Auto_Count_Id+"/"+Auto_Count_Total+" ]");
      }
    });

and

    <2>
    // repaint(long time, int x, int y, int width, int height) 
    frame.repaint(10,0,0,500,300)

They don't work either, it only repaints after the task is finished, what can I do ?

+3  A: 

Don’t do your heavy work in Swing’s event thread. Create a new thread for your calculations so that Swing can use its thread to repaint the GUI.

Bombe
A: 

Take a look at the SwingWorker class.

ninesided
A: 

If you are doing work in the AWT Thread then the repaint() and setTitle() are queued and will be invoked after you are finished processing. You should take the advice of @Bombe and do your processing in a thread other than the AWT thread then use the SwingUtilities.invokeLater() from the other thread to update the title.

Clint
A: 

Yea, SwingWorker is the way to go.

If you are using Java 6, it comes with a version of SwingWorker that contains publish() and process() methods. These methods are there so that you can publish data as you are executing your long running task, and then you can publish that data in the Event Dispatch Thread.

What you can do is create a SwingWorker subclass with the long running task defined within. Then, inside that long running task, publish the JFrame title string using the publish() method. Overwrite the process method to set the title of your JFrame.

Here's the SwingWorker API doc for Java 6

Jose Chavez