Skip to content
Advertisement

GridBagLayout gridwidth doesn’t work as expected

I’m working with the java swing LayoutManager GridBagLayout, and ran into this problem. I want a layout like this

ACC
BB

But get a layout like this

ACC
B

A and B take up the same number of columns despite B having a gridwidth of 2 where A’s gridwidth is 1. I don’t think there can be a vanishingly small column between A,B and C because C starts in column 1. The problem does not occur if C’s gridwidth is 1 instead of 2. I’m baffled by the output.

Why is this happening / how can I fix this?

JavaScript

Advertisement

Answer

I know how you feel… It seems GridBagLayout reduces the width of columns to 0 when they are not filled by 1×1 component. I don’t know if the following is the most efficient solution, but it works (by adding two dummy JPanel to “inflate” columns 1 and 2):

JavaScript
Advertisement