I got the following GridBagLayout:
The first and the last column with the JLists are set to grow: 1
. I wan't these JLists to always have the same width, no matter how wide the contained items are. How can I achieve this?
I already tried it with list.setFixedCellWidth(100)
, but then the surrounding JScrollPane does not work anymore.
You need to set prototype cell value on the JLists
jList.setPrototypeCellValue("asdf");
Set the same text on both JLists. JList will then "pretend" that all the items are as wide as provided text. This width will be used to determine if scrollbars are needed so you should find the longest item you are adding to the JList and set the prototype as that.
Setting fixed cell width with setFixedCellWidth(int)
will work too, but you will have to calculate the width of the widest text yourself.