This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Problem with GridBagLayout and a vertical layout of JPanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with GridBagLayout and a vertical layout of JPanels" Watch "Problem with GridBagLayout and a vertical layout of JPanels" New topic
Author

Problem with GridBagLayout and a vertical layout of JPanels

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I'm trying to get a handle on the use of GridBagLayout and am having a problem getting one particular layout to work the way I want.
I'm using Java 6 (jdk 1.6.0_06). I've read through the API pages for this layout manager, as well as the tutorials provided by Sun (and others), but I think I must still be missing something. I hope I can explain this well enough in text. I can make my sample program available if anyone needs to see it.

I have it set up as follows:

I have a main content panel (which is inside a JFrame). The main content panel is using GridBagLayout.
Within the main content panel, I want to stack three inner panels, which should go top to bottom, each panel taking up the full width of the containing panel. The second of the three panels changes size based on a selection that is made in the first of the three panels (the first panel contains a ComboBox, and a selection there determines what is displayed in the second content panel).

The third content panel doesn't change size.

The important point is that I want the panels to sit as high up as they can. The second panel should sit right up under the first panel, and the third panel should sit right up under the second panel. Any extra vertical space should be left at the bottom of the containing panel (by the way, I am calling validate and repaint after changing the size of the second panel, and that is working - and is not the problem I'm having...).

It's pretty easy to get the first panel to sit up at the top of the main content area, and not too bad getting the second panel to sit up under the first one. However, I'm having a hard time getting the third panel to scoot up under the second one.

Here are the GridBagConstraints I have set for the first panel:

For the second panel, I make the following changes to the constraints:For the third panel, I have to do the following to get it to sit up under the second panel: Why do I have to use such a high weighty? This causes it to work, but I wonder what would happen if I wanted to stack a few more panels under there?
Note that if I use a smaller weighty value, then the third panel will sit somewhere in the vertical space between the second panel and the bottom of the containing frame (where, exactly, depends on the weighty value).

Thanks so much for any insight.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
For layouts, it's almost always better if we can run the code and see the GUI. I suggest that you create and post a very small program that compiles and runs and demonstrates your problem. You may wish to give your JPanels titled borders or fill them with a color to allow others to distinguish them.

The key here is small. Only add code that relates to the problem and nothing else, as this simpler program will be easier and quicker for others to understand.

Much luck!
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Fair enough. I realize that the whole issue of the second content panel changing size is not part of the issue, so I've done away with that. This program has three inner content panels. In it's present state, the third panel (the Blue one) floats about midway in the available vertical space, while I want it to sit right up under the second content panel.

As I said, if I set the weighty value really high when adding the third content panel, it sits up where I want it, but this just doesn't seem like the right solution.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
One possible solution is to nest JPanels and layouts. For instance, the content pane's layout could be BorderLayout, and it could hold a JPanel that uses GridBagLayout in the BorderLayout.NORTH position. Then this gridBag-using JPanel could hold all of your contentPanels. This will push all the content panels to the top of the GUI.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I'm sorry, perhaps I'm missing something. I don't see how that answers my question.

I have a container which uses GridBagLayout, and I want to place three content panes inside it, in a vertical stack, such that they gravitate to the top of the container. Can this not be done using GridBagLayout?

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
I'm sure it can be done, but I've never had the patience to try and figure out how all the constraints work together.

I would just use a BoxLayout which was designed for this type of layout.


Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Thanks, but I'm trying to figure out how to do it with GridBagLayout. I was told it was easily doable, and I'm just trying to find out how.
The real code is far more complicated than I've shown, and I don't have the kind of control necessary to change it too much.

Perhaps GridBagLayout can't do it?

I'll try the Box layout and see what they think about that though.

Thanks,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
Think I got it. The trick is to set the weightY only on the last component added, so I borrowed from the BoxLayout to add a dummy component at the end. Of course you could add the weightY to the last real component, but this assumes you know exactly how many components you want to add. I'm assuming you have a dynamic panel where you can add a random number of components.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Mark E Hansen wrote:Fair enough. I realize that the whole issue of the second content panel changing size is not part of the issue, so I've done away with that. This program has three inner content panels. In it's present state, the third panel (the Blue one) floats about midway in the available vertical space, while I want it to sit right up under the second content panel.

As I said, if I set the weighty value really high when adding the third content panel, it sits up where I want it, but this just doesn't seem like the right solution.

Works fine for me by leaving weighty at its default value (0.0) for the first two panels and setting any non-zero weighty for the last.But I agree with Rob Camick, BoxLayout is the correct choice here.

edit Glad you sorted it out yourself. Keep it up.


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

Joined: Jun 13, 2009
Posts: 2097
    
    7
What confuses me now with the original example is how the weightY is working. I thought weightY was used to increase the height of the panel. So why do we see a "gap" between the second and third panel. I would expect the the height of the second panel should be increased so the "gap" should actually be painted CYAN since that panel has increased in height. Similiarly the "gap" after the third panel should be painted BLUE since it represents the increased height of the third panel.

Am I missing something else about how this layout manager works?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Rob Camick wrote:What confuses me now with the original example is how the weightY is working. I thought weightY was used to increase the height of the panel. So why do we see a "gap" between the second and third panel. I would expect the the height of the second panel should be increased so the "gap" should actually be painted CYAN since that panel has increased in height. Similiarly the "gap" after the third panel should be painted BLUE since it represents the increased height of the third panel.

Am I missing something else about how this layout manager works?


It could be due to his setting the GridBagConstraints fill property to horizontal and not both.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Rob Camick wrote:Think I got it. The trick is to set the weightY only on the last component added, so I borrowed from the BoxLayout to add a dummy component at the end. Of course you could add the weightY to the last real component, but this assumes you know exactly how many components you want to add. I'm assuming you have a dynamic panel where you can add a random number of components.


In the real case, the component with the first and second inner panels is a class by itself. I need to sub-class that to create a more specialized component which not only shows the two inner panels, but adds the third panel. The original component still needs to work as it always has. I can change the original if I need to, but I was told to use GridBagLayout, as it would easily be able to handle this type of layout.

I guess adding a high weight value is the only answer (as far as using GridBagLayout goes).

Thanks,

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I tried the BoxLayout example provided earlier, and it is interesting. I see that to use this, we need to set the maximum size on each of our inner panels. This will make it harder to use, as the inner panels are actually pre-existing components which need to continue to work in other places in the application, and I'm worried about changing them too much. Of course, I could just place those component inside my own panels and set the size on those...

However, the reason for this topic was to see if I understood the use of GridBagLayout (in the limited way I'm trying to use it here) and to question why it seemed that to make it work I had to use a really large weighty on the last component added to the container (I guess that's just the way it is).

Thanks for all the help (and all the work on the examples). I very much appreciate it!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
as the inner panels are actually pre-existing components which need to continue to work in other places in the application, and I'm worried about changing them too much. Of course, I could just place those component inside my own panels and set the size on those...


Components can only be added to a single parent container.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Rob Camick wrote:
as the inner panels are actually pre-existing components which need to continue to work in other places in the application, and I'm worried about changing them too much. Of course, I could just place those component inside my own panels and set the size on those...


Components can only be added to a single parent container.


I didn't say otherwise. I think you misunderstood me. The application has a FooWidgetPanel which is used in several places. One such place is a panel that I'm working with. If I have to make changes to FooWidgetPanel in order to make it work in my specific case, I would have to be worried about side-effects of that change in the other places where it is used.

Sorry if I was unclear.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
It could be due to his setting the GridBagConstraints fill property to horizontal and not both.


Yes, using BOTH works.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2097
    
    7
The application has a FooWidgetPanel which is used in several places.


So you have multiple instances of the class then.

So there should be no problem to set the maximum size of the instance that you want to add to the BoxLayout. For the other instances that you add to other panels you just leave the code as is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with GridBagLayout and a vertical layout of JPanels
 
Similar Threads
Looking for layout advice
GUI - tile
JCheckBox Allowable Selections
Trying to resolve some odd resizing behavior of objects in GridBagLayout
FlowLayout Display Problems