Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
treePanCon.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:
enter image description here

This a screenshot after adding components to the nested JPanel:
enter image description here

like image 752
FadelMS Avatar asked Sep 28 '26 01:09

FadelMS


2 Answers

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.

like image 80
Ken Blair Avatar answered Sep 29 '26 15:09

Ken Blair


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.

like image 34
Ed Staub Avatar answered Sep 29 '26 13:09

Ed Staub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!