Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel and LayoutManager

 
Tom Sakra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

My program fills a panel with checkboxes dynamically and I want the panel to stretch accordingly to the amount of content,
this works fine as long as I keep the window at the same size at runtime as I do when I'm working in NetBeans. However, if I
try to maximize the window, the panel suddenly gets greedy and takes up a lot of extra space beneath the checkboxes, without
actually putting anything else in there.

Any suggestions as to how I could solve this? I've been using a BoxLayout with the vertical resizable on the aforementioned panel.
 
Stephan van Hulst
Bartender
Pie
Posts: 5371
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you will have to decide what you do want to do with the extra space.
 
Rob Camick
Ranch Hand
Posts: 2539
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I try to maximize the window


Thats what maximizing does, it maximizes the vertical and horizontal areas.

For more help create a SSCCE that demonstrates the incorrect behaviour.
 
Tom Sakra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but it doesn't maximize it completely either. It just uses about 200 pixels more than it should when I maximize the window.
So there's still room for it to stretch more, which doesn't make any sense as to why it just randomly stops, but I'd rather it stopped
stretching after the panel was filled. So is there any way to decide the size of the panel without using the vertical resizable option?

Edit:
Thanks for the help guys, but I just did what I asked for in the last line above :P Just added the preferredSize (height) of the given
checkboxes to a variable and adjusted the panel size to that variable, and using the BoxLayout with the page axis.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use layout managers correctly, you should never have to adjust a size manually.

I encourage you to post a SSCCE, as Rob already asked, so others here can study it and advise you how to improve it.
 
Tom Sakra
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course not, but I find creating a relatively large GUI in NetBeans is a bit of a wrestle itself.
Since I'm using NetBeans to create the GUI, I'm pretty sure that creating that exact part
would've been easier manually. But I'm not familiar with using custom components in the NetBeans
GUI editor, so this will have to do for now. I might come back to it later if the schedule allows for it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic