• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updating the JLabel

 
Nagaseshagiri poola
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to update the JLabel.
class variable label
JLabel label = new JLabel();
public void actionPerformed(ActionEvent e) {
String t1 = "Here Iam";
String t2 = "Where are you";
String t3 = "Feel it";
label.setText(t1);
Thread.sleep(5000);
label.setText(t2);
Thread.sleep(5000);
label.setText(t3);
Thread.sleep(5000);
}
i see only the last string and that is "Feel it".
help me !!!
 
David Crossett
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not 'repainting' your labels after you change them. If you change label text or Components positions, you have to 'repaint' the container - this is done one of two ways. Either

or you can write

where 'c' is the component to repaint (label in your case). Anyway, the point is that you are not re-displaying your contianer after you change it.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What David says is correct, but it still won't fix the problem. You should never call sleep(), or perform any other time-consuming operation, in an event handler. While an event handler is running, no other GUI processing -- which includes other event handling and all painting -- can occur. So adding validate() calls would have no effect -- the painting thread is the same one that's blocked in the event handler, so the screen wouldn't be updated.
To make time-dependent actions happen in a GUI, you must use a Thread or Timer. For example, in the present case, you could just say
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic