File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JPanel and LayoutManager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel and LayoutManager" Watch "JPanel and LayoutManager" New topic

JPanel and LayoutManager

Tom Sakra

Joined: Oct 09, 2010
Posts: 8

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

Joined: Sep 20, 2010
Posts: 4627

Well, you will have to decide what you do want to do with the extra space.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2452
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

Joined: Oct 09, 2010
Posts: 8
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?

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

Joined: May 03, 2008
Posts: 5115

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.

luck, db
There are no new questions, but there may be new answers.
Tom Sakra

Joined: Oct 09, 2010
Posts: 8
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.
I agree. Here's the link:
subject: JPanel and LayoutManager
jQuery in Action, 3rd edition