aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes GridBagLayout cuts off FlowLayout's second row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GridBagLayout cuts off FlowLayout Watch "GridBagLayout cuts off FlowLayout New topic
Author

GridBagLayout cuts off FlowLayout's second row

Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
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

Joined: May 03, 2008
Posts: 4658
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Andrew Cho
Ranch Hand

Joined: Jun 29, 2011
Posts: 50
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

Joined: Oct 16, 2003
Posts: 1088
    
  10

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).


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2215
    
    7
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

Joined: Jun 29, 2011
Posts: 50
Very helpful, guys.
Rob, your WrapLayout is exactly what I was looking for.

Thanks
Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GridBagLayout cuts off FlowLayout's second row