How to make Component in GridBagLayout
take up same width? I have tried GridBagConstraint.weightx
, but it doesn't work.
static void test3() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(480, 360);
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
String[] a = {
"Lorem ipsum dolor sit amet",
"consectetur adipisicing",
"elit, ",
"sed do",
"eiusmod",
};
for (int i = 0; i < 5; i++) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = i % 3;
c.gridy = i / 3;
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
f.add(new JButton(a[i]), c);
}
f.setVisible(true);
}
static void test3() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800, 360);
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
String[] a = {
"Lorem ipsum dolor sit amet",
"consectetur adipisicing",
"elit,",
"sed do",
"eiusmod",
};
for (int i = 0; i < 5; i++) {
JButton test=new JButton(a[i]);
GridBagConstraints c = new GridBagConstraints();
c.gridx = i %3;
c.gridy = i /3;
c.fill = GridBagConstraints.HORIZONTAL;
test.setPreferredSize(new Dimension(250,25));
f.add(test, c);
}
f.setVisible(true);
}
You need to set a preferred size for the buttons. this will give you desired outcome