1

I have a JLabel that I require to change dynamically when a button is pressed. I am using JLabel.setText(s)

However, it is not working. I've tried:

JLabel.repaint()
JLabel.validate()
JLabel.revalidate()

The thing I find strange is that it works fine when I don't set the size of the JLabel using any of:

JLabel.setPrefferedSize()
JLabel.setMinimumSize()
JLabel.setSize()

Can anyone help me with why it isn't working after I set one of the size properties?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Please, add [SSCCE](http://sscce.org) to your question. – MockerTim Oct 04 '11 at 15:54
  • It's very hard to guess what is wrong (and that's all we can do) based on the limited data provided. If none of the answers below answer your question satisfactorily, I too recommend that you create and post an [SSCCE](http://sscce.org). – Hovercraft Full Of Eels Oct 04 '11 at 22:27

4 Answers4

2

What your button is doing is probably a very long task and you want to keep the user informed about the process.

To do this you have to create a new Thread which performs the long task and when you set new text to your label, wrap the label.setText() statement into an invokeLater():

public void buttonAction()
{
   new Thread(new Runnable()
   {
       public void run()
       {
           // Here your long task

           // When you want to call Label.setText(), do it like this:
           SwingUtilities.invokeLater(new Runnable()
           {
               public void run()
               {
                   label.setText("Loading 1/13...");
               }
           });

           // Here another part of your task....

           SwingUtilities.invokeLater(new Runnable()
           {
               public void run()
               {
                   label.setText("Done!");
               }
           });
       }
   }).start();
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1

For sizing components in Swing, ultimately the layout of the parent container has the final say on the size of the component.

For example, in FlowLayout, calling setPreferredSize(), setSize(), or setMaximumSize() on a component all have the result of giving the label that size, and not sizing over it. However, with setMinimumSize() it will start at that size, then size above it.

For BorderLayout, if the component is added to the center, none of the sizing methods affect the component - it just takes the maximum amount of space available.

If you set the layout of the parent container to null you can explicitly size components, however, you will lose the automatic resizing that you have with other layouts.

Nate
  • 16,748
  • 5
  • 45
  • 59
0

Figured out my problem,

I was using:

JLabel.setPreferredSize(100, JLabel.getheight());

I changed to:

JLabel.setPreferredSize(100, 22);
  • Most probably `getHeight()` returned `0` so your label didn't have the space to display correctly, and was thus not showing at all. – Didier L Mar 14 '13 at 14:37
-1

JLabel.setPreferredSize(100, JLabel.getheight());

As you are setting the preferred size here, the height of JLabel must not have been previously set :), if you try the same line after setting the height, I think it should work well.

Also sometimes JPanel<xx>.updateUI(); is required to refresh the displayed UI.enter code here

  • updateUI is _not_ for use by application code! And [using setPreferredSize](http://stackoverflow.com/a/7229519/203657) is extremely suboptimal (but wouldn't get a downvote in itself in the context of the QA which is a bit oldish) – kleopatra Mar 21 '13 at 15:58
  • what exactly updateUI is used for then? I have seen it work for refreshing my panels but I may be totally wrong. – dguitarbite May 05 '13 at 06:31