aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Absolute layout on nested panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Absolute layout on nested panel" Watch "Absolute layout on nested panel" New topic
Author

Absolute layout on nested panel

David A. Smith
Greenhorn

Joined: Sep 08, 2010
Posts: 9

I have a GUI in which I want absolute positioning within a subpanel. I'm using GridBagLayout elsewhere, including the parent. When I set the layout manager to null on a top level panel, everything works as I'm expecting. But, when trying to do the same on a subpanel, I get a blank panel (no visible controls). I've reduced this down to a code fragment (based on examples from the web) that can demonstrate this both ways by setting an (obviously named) boolean variable. Here's the code fragment:



So, what I'm I doing wrong, or what am I missing?

Thanks for any and all tips.

Dave.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Your outer panel has a default FlowLayout, which displays children at their preferred sizes. your inner panel, having a null -- not Absolute -- layout, has a preferredSize of [0, 0].

Set a BorderLayout to parent and see the difference.


luck, db
There are no new questions, but there may be new answers.
David A. Smith
Greenhorn

Joined: Sep 08, 2010
Posts: 9

Thanks Darryl for the reply. I took your suggestion and came up with the following conclusions.

With the child panel implicitly set to absolute positioning (via "null" LayoutManager), components were displayed properly when the parent panel's layout manager was set to BorderLayout (just as you suggested).

I was hoping that the layout manager setting for the child (including the null setting) would apply to that panel (only!) and not have it (the child) alter its behavior based on the parent's setting. Argh! But, the rest of my application is currently based on the GridBagLayout. Specifically, the parent to my child panel (in this particular case) is (also) GridBagLayout. The child does NOT behave correctly when the parent is GBL, but it DOES behave correctly when BorderLayout. Again, argh!

parent = null -> doesn't work
parent = GridBagLayout -> doesn't work
parent = FlowLayout -> doesn't work (but then again, who cares? I've never used FL in any real application)

BUT...

parent = BorderLayout -> works fine
parent = GridLayout -> works fine

So, here's my solution:

Parent stays set to GridBagLayout keeping with my existing design. In fact, there are already multiple layers of GridBagLayout at that point (I think there are 2 layers). I've introduced a new *intermediate* panel that is set to BorderLayout that is otherwise useless. Then there is the child (grandchild?) panel set to null (absolute). The child looks only one level up, sees the intermediate BorderLayout, and behaves correctly. The layers above that still behave according to the GridBagLayout that I intended.

Other than the wasted layer of JPanel, I'm back in business. Gotta love it!

Thanks again, Darryl, for your advice.

Dave.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

I think you're still missing the point. It's the layout manager that computes the preferredSize for a JPanel. Since you are (unfortunately!) using no layout manager -- and don't keep calling it Absolute layout -- you are responsible for computing the preferredSize or housing the panel in a container with a layout that doesn't necessarily display a child component at its preferredSize.

You might like to read Rob Camick's blog post on Drag Layout and take a look at the section of the code that computes the layout size.
 
 
subject: Absolute layout on nested panel