I have a GUI that has a JScrollPane (scrollPane) and a JPanel (headerPanel) inside another JPanel (mainPanel). I used GridBagConstraints with the GridBagLayout for mainPanel and a FlowLayout for headerPanel. I also weighted the components so scrollPane takes up the unused space of headerPanel. The only problem is that when the Flowlayout moves components in headerPanel to another row (as it should), the GridBagLayout of mainPanel cuts off the additional rows.
So my question, is there a way to have the GridBagLayout accommodate for extra rows of a FlowLayout, when the component with the FlowLayout is weighted less than the other components?
BorderLayout will have the same issue - this is because, when BorderLayout is used, the Center component gets all the max/extra/leftover space to occupy - this is same as giving more weight to the scrollpane component.
However, in GridBagLayout, you can vary the weight - for example, the GridBagLayout Tutorial specifies the following:
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. The default weight is 0.0
I have tried the following and am getting what you are looking at:
However, when the frame is maximised, the header panel shows some empty space. My guess is that a call to setPreferredSize on the headerPanel will yield the same behaviour (without weights specified).