File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Updating the JLabel" Watch "Updating the JLabel" New topic

Updating the JLabel

Nagaseshagiri poola

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";
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

Joined: Jul 08, 2003
Posts: 24199

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:
subject: Updating the JLabel
It's not a secret anymore!