Click to See Complete Forum and Search --> : Help: JLabel setText not updating


Kin Wong
September 27th, 1999, 08:28 AM
Hi,

I'm using a JLabel at the bottom of my application as a status bar where I'm trying to inform the user that a certain operation is taking place, such as loading a file, etc. An excerpt of the inner class code is shown below. The problem I'm running into is that the setText is not updating statusLabel (even though the print statement clearly shows that I am setting the JLabel) until the process is completed. Any ideas why? I don't want to run the process under a seperate thread because I dont' want the user to continue until the process is completed correctly.

public class AppJFrame extends JFrame {

JLabel statusLabel = new JLabel(" ");

....

class ButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
statusLabel.setText("Loading Info ... ");
System.out.println("Status Bar Text = " + statusLabel.getText());
AppJFrame.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
// create a process that runs a C program. The println shows that
// statusLabel = "Loading Info ... ", but the JLabel is not updated
// until the process completes.
}
}

}


Thanks for any info,
Kin Wong

dan_pretty_boy
September 27th, 1999, 08:51 AM
did you try calling repaint on the frame or revalidate()

Kin Wong
September 27th, 1999, 04:01 PM
I did some research and was able to solve my problem. Hopefully it'll help those who might run into the same problem. Apparently, Swing component repainting is managed by a RepaintManager class which intercepts all paint requests and calls invokeLater() to process the pending requests on the same dispatch thread. Therefore you have no control over exactly when the repainting occurs. However, the method paintImmediately() can be used to cause a Swing component to get updated immediately. For more info see: http://java.sun.com/products/jfc/tsc/special_report/painting/painting.html

So in my code, I added the following line immediately after the setText():

statusLabel.paintImmediately(statusLabel.getVisibleRect());

Kin Wong