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
.
views:
108answers:
2
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
2010-07-21 05:15:58
I have already coded to call the repaint method. But its not getting the call after setVisible property is set to true.
Joe
2010-07-21 06:25:29
If so try like setVisible(false), set the value then setVisible(true)... Give it a try...
venJava
2010-07-21 06:51:12
No. it still showing the old value.
Joe
2010-07-21 07:31:26
-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
2010-07-21 14:04:34
+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
2010-07-21 14:07:31