• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GridBagLayout cuts off FlowLayout's second row

 
Andrew Cho
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

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?

here is my SSCCE:


As you can see, not all of the buttons show up, but if you expand the JFrame window, the buttons move to the first row of the FlowLayout.

Thanks for any help,
Andrew
 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disclaimer: I haven't tried your SSCCE.

Given the description, I would try using a BorderLayout with the headerPanel, with a FlowLayout, added to NORTH and the mainPanel, with a GridBagLayout, added to CENTER.
 
Andrew Cho
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks for your reply Darryl.
Unfortunately, the BorderLayout has the same problem, where the buttons in the headerPanel disappear into the second row of the FlowLayout.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Rob Camick
Ranch Hand
Posts: 2599
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


The problem is that the FlowLayout doesn't change the preferred size when components wrap.

Try the Wrap Layout.
 
Andrew Cho
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very helpful, guys.
Rob, your WrapLayout is exactly what I was looking for.

Thanks
Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic