• Post Reply Bookmark Topic Watch Topic
  • New Topic

Weirdness when changing CENTER of a JPanel with BorderLayout

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I've run into a bit of a problem. I have a class that's basically an extension of JPanel.

It uses a Border layout. Now the PAGE_START and PAGE_END sections are constant, and always show properly.

If the JPanel itself is called mainPanel, then the CENTER part has a middle section with a JPanel called middlePanel. MiddlePanel has a single JLabel (called middleLabel) that initially starts out with no text.

All well and good. However, if sometime after the initial layout I use middleLabel.setText("some text") things don't show properly. I can only see the top pixel or two of the text, as if the CENTER section didn't expand vertically to accomodate the fact that there is now text present.

I've tried doing validate and revalidate on both mainPanel and middlePanel, but it doesn't seem to help.

However, if JLabel starts out with text, everything seems fine. This isn't a practical solution though, because there will be times when I need to have text in there, and times when I will need it to be blank.

What am I doing wrong?

Thanks in advance...
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your description sounds like expected behavior. And I infer from it that the JPanel
extension (mainPanel) is in a top–level container that has had pack called on it to set
its size compactly. To avoid this resizing trouble I usually instantiate JLabels with a
blank space and/or with a minimal height:

One or both seem(s) to allocate enough space for future calls to setText.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That's more or less what I wound up doing (well, slightly different, but with the same idea)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!