• Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel: how do I set it to a fixed width?  RSS feed

 
Kentaro Shinbashi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JLabel that displays a numerical value; the value changes during the program, from 0 to 100 %.

When the value changes, the label and the components that are next to it jump around a little bit, and it looks bad.

I've tried setting the text of the JLabel to a formatted string with the format width being big enough to hold this, but it still jumps around.

How can I tell the JLabel to hold its size, either dimensionally or number of characters?

Thanks.

Kentaro.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are a number of things you can do, but they depend on the layout manager used

perhaps the simplest is to set the label's preferredSize

run this, then uncomment the indicated line, recompile/rerun

 
Kentaro Shinbashi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, thanks.

This works perfectly. I had to add about 6 or 7 to the preferred size, but it only took a little bit of trial and error.


Thanks for your helpful suggestion.


Kentaro
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!