File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Updating the JLabel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating the JLabel" Watch "Updating the JLabel" New topic
Author

Updating the JLabel

Nagaseshagiri poola
Greenhorn

Joined: Oct 18, 2003
Posts: 25
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

Joined: Feb 05, 2003
Posts: 102
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.


David Crossett
-nothing important to say, but learnin' plenty-
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating the JLabel