views:

108

answers:

2

I've a JDialog with a JLabel. I want to change the value of label every 10 seconds. How can I redraw/repaint the JDialog with updated value in JLabel.

A: 

You can use Timer and TimerTask to schedule for every 10 seconds, in which you've to furnish the JLabel's Text value. After that you've to refresh the current window with repaint() method. Now you'll get the JLabel's Text value updated...

venJava
I have already coded to call the repaint method. But its not getting the call after setVisible property is set to true.
Joe
If so try like setVisible(false), set the value then setVisible(true)... Give it a try...
venJava
No. it still showing the old value.
Joe
-1, you should NOT be using a util.Timer and util.TimerTask to schedule updates to a Swing component. All Swing components should be updated on the EDT.
camickr
+1  A: 

Use a Swing Timer to schedule the updates to the label. Then all you do is

label.setText(...);

and the label will be repainted automatically. There is no need to invoke repaint().

You problem may be that you are trying to start the Timer after you display a modal JDialog. In this case the code does not execute until the dialog is closed. So you need to make sure you start the Timer before using dialog.setVisible(true).

If you need more help then post your SSCCE showing the problem.

camickr