• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with GridBagLayout and a vertical layout of JPanels

 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It could be due to his setting the GridBagConstraints fill property to horizontal and not both.



Yes, using BOTH works.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic