Is GridBagLayout broken in JPanel

I've been trying to align my components to GridBagConstraints.WEST in a JPanel, but they all remain centralised. I'm extremely confused. The alignment works fine when the components are used in a JFrame. Can anyone help me or do I need to submit a bug report? I've read that perhaps placing the JPanel within another JPanel will solve the problem ,but I'm not fond of hacks. If it's a bug it's a bug. I'm using JDK1.5.0_10.

Any help/information would be greatly appreciated.

[494 byte] By [heavydawsona] at [2007-11-26 16:42:04]
# 1

I've read that perhaps placing the JPanel within another JPanel will solve the problem ,but I'm not fond of hacks.

Personally I'd consider the use of GridBagLayout to be an ugly hack instead of breaking down a UI into logical sections.

GridBagLayout was intended for visual UI builders (spit), not really for hand-cranked code. It's easy to foul up with it.

If you really must use a complex layout (ie you really can't fragment your UI logically) then google for TableLayout, which is much more user friendly.

itchyscratchya at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for reply, and I'll definately look into TableLayout though I'm quite comfortable with GridBagLayout and don't necessarily agree with your view.

Personally I use seperate gridbaglayout for each container in the frame. Its not so bad as long as you keep the component count down in each gridbaglayout.

All that aside, the question I posted in the title of the thread is still unanswered....Is GridBagLayout broken when used for a JPanel. It certainly appears to be so....

heavydawsona at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 3
Chances are very, very small that this a java bug.
es5f2000a at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 4

As far as I am aware GridBagLayout itself is centred in the panel, the components added to it can be set to align to the left, but only if the layout fills the entire panel -- To prove this to yourself, set the layout of the main panel to FlowLayout(FlowLayout.LEFT) for instance, and add a new Panel with it's layout set to gridbag, and see that it will be left aligned.

I don't advise that you use this in production code, there are better alternatives out there.

macrules2a at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 5

Thanks for all the feedback, but I'm still under the impression that GridBagConstraints (it seems to be just anchor and fill) are being disabled or over-ridden by JPanel.

When the exact same constraints work perfectly for a JFrame, I'm assuming they should work also for a JPanel. I've submitted a bug report, and will post any information on this thread if anything is found.

In the meantime, thanks for all the help and advice. It seems I'll have to investigate other layout managers immediately!

heavydawsona at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Thanks for all the feedback, but I'm still under the

> impression that GridBagConstraints (it seems to be

> just anchor and fill) are being disabled or

> over-ridden by JPanel.

As I said, chances are excellent that you are wrong.

Feel free to post an SSCCE and people will show you your error,

if there is one.

> In the meantime, thanks for all the help and advice.

> It seems I'll have to investigate other layout

> managers immediately!

http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

is a good place to start.

es5f2000a at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I've been trying to align my components to GridBagConstraints.WEST in a JPanel, but they all remain centralised

> If it's a bug it's a bug

Of course its not a bug.

If you would read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use GridBagLayout[/url] you will find the explanation. It deals with the weightx and weighty constraints.

Thats why we generally recommend using other layouts that are easier to understand and not a fickle to use when you get a single constraint wrong.

camickra at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 8
It's no wonder it takes two months or so for the bugs I raise to get into the system when people just send in bugs without checking them first :o(A little less hasty next time, please...Mind you, getting a bug into the database isn't even 1% of the battle ;o)
itchyscratchya at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...
# 9

;-D

The weightx and weighty solution mentioned above worked.

I'll see what I can do to retract the bug report.

Sorry for all the consternation caused, and thanks for the solution.

All the help was greatly appreciated.

I'll leave any future issues sit on the forums for a week before I file any further bug reports.... ;-)

heavydawsona at 2007-7-8 23:09:08 > top of Java-index,Desktop,Core GUI APIs...