GridBagLayout and JMenuBar problems  
Author Message
Laird Nelson





PostPosted: 2004-3-3 23:27:00 Top

java-programmer, GridBagLayout and JMenuBar problems Here's another interesting one. I looked on the Bug Parade to see if
this was present, but rapidly drowned in the sea of totally unrelated
results I got back from the brain dead search capabilities.

I built up a JPanel using GridBagLayout and various components. Works
fine; I'm quite used to GridBagLayout and its quirks. I'm using the
Java look and feel with native decorations turned off, so GridBagLayout
respects my panel's minimum size constraints.

I had been testing this layout in a JFrame without a JMenuBar, just to
validate the layout. Now when I added a JMenuBar to the JFrame, the
layout screws up when I resize the panel down to its smallest size.
It's as though the GridBagLayout does not take the JMenuBar's dimensions
into account when resizing happens. In my case, a couple of buttons
shrink in height, a text field bizarrely grows to 1.5X height, and
another text field overlaps it. None of these problems show up unless
the menu bar is in place.

Again: a known bug? Or should I submit it?

If there's any interest I'll whittle my code down to a SSCCE. Then I'll
try it with native window decorations turned on to see if that's where
the problem lies.

Laird
 
Laird Nelson





PostPosted: 2004-3-4 1:04:00 Top

java-programmer >> GridBagLayout and JMenuBar problems Laird Nelson wrote:
> I had been testing this layout in a JFrame without a JMenuBar, just to
> validate the layout. Now when I added a JMenuBar to the JFrame, the
> layout screws up when I resize the panel down to its smallest size. It's
> as though the GridBagLayout does not take the JMenuBar's dimensions into
> account when resizing happens. In my case, a couple of buttons shrink
> in height, a text field bizarrely grows to 1.5X height, and another text
> field overlaps it. None of these problems show up unless the menu bar
> is in place.

For those who care, my workaround is to add a vertical strut in the
middle of my GridBagLayouted panel whose weighty is set to a positive
value. The initial height of this strut is set to the height of the
JMenuBar installed on the panel's JRootPane ancestor. This means that
after a JFrame.pack() operation, this strut will add exactly enough
space so that when someone shrinks the frame that extra space will be
eliminated without causing any of the other components to be shrunk.

I recalculate the strut height on addNotify() and setUI() so that it is
correctly sized according to the L&F in place at the time.

Laird