2

I am trying to draw a panel with buttons, that looks something like this:

enter image description here

I create the "buttonPanel" like this:

buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0;
gbc.weighty = 0;

gbc.gridx = 0; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A0"), gbc);

gbc.gridx = 1; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A1"), gbc);

gbc.gridx = 2; gbc.gridy = 0;
gbc.gridwidth = 2; gbc.gridheight = 2;
buttonPanel.add(new JButton("A2"), gbc);

gbc.gridx = 0; gbc.gridy = 2;
gbc.gridwidth = 3; gbc.gridheight = 2;
buttonPanel.add(new JButton("A3"), gbc);

gbc.gridx = 3; gbc.gridy = 2;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A4"), gbc);

But the result looks like this:

enter image description here

As button "A3" has a gridwidth=3, it should reach the middle of button "A2"?

Any help?

Edit1: Increasing the panel size, or setting a longer text on the buttons, does not change anything:

enter image description here

Edit2: Setting the weightx=1 for button "A2" and "A3" helps:

enter image description here

This is acceptable for me, although it would be nice if button "A4" would have the same width as button "A0" and "A1"

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

2

Set for A2 and A3 the weightx to 1.0.

brummfondel
  • 1,202
  • 1
  • 8
  • 11
1

although it would be nice if button "A4" would have the same width as button "A0" and "A1"

You can't have a cell take up a partial space of another cell.

So you need to "fake" it by creating 4 (invisible) dummy components. This allows you to define a grid of 4 columns.

Then A2 would have a gridwidth of "2" and A3 a gridwidth of "3". The others would have a gridwidth of 1. So now each row has a total cell widths of 4 to match the dummy cells.

Check out: Why does this GridBagLayout not appear as planned? for an example of this approach.

Or an easier option is to use the Relative Layout. It allows you to give components a relative size. So you would need two panels. In the first the components would have relative sizes of 1, 1, 2 and the second 3, 1.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • If an alternative to GridBag Layout is possible, MigLayout is a very good solution for this: http://www.miglayout.com/ – brummfondel Dec 23 '17 at 16:25
  • @brummfondel, this has nothing to do with my answer. Post it in your answer or as comment to the question. – camickr Dec 23 '17 at 16:27
  • You mentioned Relative Layout as option and I MigLayout as comment to your comment. – brummfondel Dec 25 '17 at 10:55
  • @brummfondel, Please remove your comments. It has nothing to do with my answer. I did not ask for suggestions of how to solve the problem. The OP asked for suggestions. So if you think it is a good suggestion, then make the suggestion in your answer not mine because I don't like MigLayout and would therefore not recommend it. – camickr Dec 25 '17 at 15:24