Skip to main content

a bug using nested GridBagLayouts ?

No replies
durden
Offline
Joined: 2006-06-09

Hallo,

if I am nesting JPanels using the GridBagLayout the component width are not calculated correctly. In the following example the component width is not evenly distributed:
Any hints to solve this?

best Regards,
Thomas

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class GridPanelTest {

public static void main(String[] args) {

GridBagConstraints gbc = new GridBagConstraints();

JPanel pane = new JPanel(new GridBagLayout());

JPanel pane1 = new JPanel(new GridBagLayout());

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;

JPanel fix = new JPanel(new GridBagLayout());
fix.add(new JButton("test1"), gbc);

pane1.add(fix, gbc);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;
pane.add(pane1, gbc);

JPanel pane2 = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;

pane2.add(new JButton("test2"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;

pane2.add(new JButton("test3"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;
pane.add(pane2, gbc);

JDialog dlg = new JDialog();
dlg.setContentPane(pane);
dlg.pack();
dlg.setVisible(true);
}
}