• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Absolute layout on nested panel

 
Greenhorn
Posts: 9
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
David A. Smith
Greenhorn
Posts: 9
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic