Periodically update JLabel in GUI - how?

Hi everybody,

I'm trying to write a status bar for my Swing application that should periodically (every 5 s or so) update the current time. I'm trying to do as follows, but the GUI blocks, when the while-loop is enabled, completely, and when removed, it blocks the whole 5 s. What am I doing wrong? As far as I could find out, the new Thread should be run by invokeAnd Wait() ...

mainWindow.setVisible(true);

try{

SwingUtilities.invokeAndWait(new Runnable(){

publicvoid run(){

while(true){

try{

Thread.sleep(500);

}catch (InterruptedException e){

e.printStackTrace();

}

mainWindow.updateStatusBar("test");

}

}

});

}catch (InterruptedException e){

e.printStackTrace();

}catch (InvocationTargetException e){

e.printStackTrace();

}

Thanks, Steffi

[1686 byte] By [steffaniea] at [2007-10-1 5:04:58]
# 1

Hi!

I created a demo programm, that updates the GUI after every 5 seconds. Sorry i don't know exactly, why your program doesn't work. I guess, because invokeAndWait() blocks, until the function run() calls return.

import java.awt.Frame;

import java.util.Date;

import java.awt.Color;

import java.lang.Thread;

import java.awt.Event;

import java.awt.Graphics;

import java.lang.InterruptedException;

import java.awt.Font;

/*

To refresh the GUI, and periodically show the current system time, there is a thread

Timer, that communicates with the class test (invoking callback()@test).

*/

public class test extends Frame

{

private Date time_stamp;

private static String[] string_numbers = { "00", "01", "02", "03", "04", "05", "06",

"07", "08", "09" };

private Thread refresh;

private Font f;

public test() {

super("Shows time...");

time_stamp = new Date();

resize(400, 100);

show();

f = new Font("Dialog", Font.PLAIN, 14);

refresh = new Timer(this);

refresh.start();

}

public boolean handleEvent(Event e) { //this is used only, if you close the window

if (e.target == this && e.id == Event.WINDOW_DESTROY) {

hide();

System.exit(1);

}

return super.handleEvent(e);

}

//creates a time string in this form: <hour:min:sec>

private String createTimeString() {

long tmp;

String time = "<";

time_stamp.setTime(System.currentTimeMillis());

if( (tmp=time_stamp.getHours())<10 )

time = time + string_numbers[(int)tmp] + ":";

else time = time + tmp + ":";

if( (tmp=time_stamp.getMinutes())<10 )

time = time + string_numbers[(int)tmp] + ":";

else time = time + tmp + ":";

if( (tmp=time_stamp.getSeconds())<10 )

time = time + string_numbers[(int)tmp] + ":";

else time = time + tmp + ":";

time = time + "> ";

return time;

}

public void paint(Graphics g) {

String current_time = createTimeString();

super.paint(g);

g.setFont(f);

g.setColor(Color.white); //sets the background color to white to clear this window

g.fillRect(0, 0, size().width, size().height);

g.setColor(Color.black); //sets the color back to black to draw the string

g.drawString(current_time, 40, 40); //draws the current time

}

public void callback() { //this function will be called from the thread Timer to "talk back" to

repaint(); //this object.

//If you call repaint(), the AWTThread will call paint() in this class!

}

public static void main(String args[]) {

test tmp = new test();

while(true) {

System.out.println("You can make hier anything without being blocked...");

}

}

}

class Timer extends Thread

{

private test parent;

public Timer(test t) {

parent = t;

}

synchronized public void run() {

while(true) {

try{

wait(5000);

}catch(InterruptedException ie) {return;}

parent.callback();

}

}

}

Hope this is what you asked...

daniel-ta at 2007-7-9 11:09:55 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
Thanks, I solved the problem by using Timer class :-)
steffaniea at 2007-7-9 11:09:55 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...