开发者

Nested JPanel with GridBagLayout

I have a JFrame with GridBagLayout. weightx and weighty values are assigned different no-zero values and GridBagConstraints.fill = GridBagConstraints.BOTH. I nested a JPanel in one of the cells and gave it a GridBagLayout too. When adding components to the nested JPanel, the cell size where the nested JPanel resides grows on all sides missing up the parent's layout. Insets and padding are not used.

How can I fix this problem?Thanks. This is an example of the GridBagConstraints values:

GridBagConstraints treePanCon = new GridBagConstraints();
treePan.setLayout(treePanGridbag);
treePa开发者_C百科nCon.fill = GridBagConstraints.BOTH;
treePanCon.weightx = 0.5;
treePanCon.weighty = 1;
treePanCon.gridx = 0;
treePanCon.gridy = 0;
treePanCon.gridwidth = 1;
treePanCon.gridheight = 1;

This is a screenshot before adding components to the nested JPanel:

Nested JPanel with GridBagLayout

This a screenshot after adding components to the nested JPanel:

Nested JPanel with GridBagLayout


That's exactly what is supposed to happen. Please explain the behavior you're looking for. As an aside the layout managers available with J2SE are less than ideal. Having done a large amount of Swing work in a past life I would highly recommend checking out JGoodies forms: http://www.jgoodies.com/freeware/forms/. Check out the whitepaper, it's easy to use and much more intuitive than GridBag.


This is probably of absolutely no use to you at this late date, but...

It looks like you probably used BOTH when laying out the two new buttons.

If the preferredSize.width on the tree is the width you want for the inner panel,
and you wanted the two new buttons to each be half the width of the tree,

set their preferredSize.width to half the preferredSize.width of the tree,
and use NONE on the two buttons.

Use VERTICAL on the tree in the inner panel. Use VERTICAL on the inner panel in the outer panel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜