• 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

Problems positioning buttons in GridBagLayout

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have spent the day touring layouts trying to achieve the following-

Three buttons in a vertical row
All the same width
The size of the buttons do not change when I resize the container
The buttons stay at the top of the container

With gridlayout, the buttons are the same size, but they resize when I change the container size
With boxlayout, the buttons keep their size and stay at the top of the container, but they are all different widths
With gridbaglayout, the buttons are all the same width, and keep there size, but move to the center of the container on resizing.

Having read what I could, I thought setting anchor would keep them pinned to the top of the container, but this is not working.

I am placing the JPane with the buttons (that I want to stay at the top) and a JScrollPane with a table in it, into another Jpanel. The buttons are WEST and the table is CENTER. That does what I want in the sense that the button panel does not get any wider when I resize and the table nicely resizes in height and width. So I don't want to break that behavior. I just want those buttons to stay at the top. The table does!

I suspect I am missing something simple. Can someone explain this to me?

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to move this thread to the GUI forum, where we usually discuss such things.

Have you come across Cay Horstmann’s GBC class? It makes GridBag easier to use. More details in the book (Horstmann and Cornell, Core Java II, vol 1). Have you tried a different anchor, eg WEST.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And since Campbell forgot the actual moving I'll do that for him
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how big is the table? does it matter, what does the cell renerer do?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Create a panel (buttons) that uses a GridLayout and add the button to the panel.
2) Create a second panel (wrapper) which be default will use a FlowLayout. Add the "buttons" panel to this panel
3) Add the "wrapper" panel to the frame. When the frame size changes the wrapper panel will change in size but the buttons panel will remain constant.
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
totally different option, have you investigated miglayout, at my last company it had become the standard layout mananger.
i have really simplified the program, replacing the table with an image, so it always takes up space.
There is just 1 little fudge the span for the picture has to be 1 row longer than you have, really odd.

 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to use GridBagLayout for the main panel too (panelMe).

Then add the button panel with usual constraints.

For the table panel, use the following constraints:
gridHeight = 2;
fill = BOTH;
weightX = 1.0;
weightY = 1.0;

This works for me and I get the desired output.
 
Jon Swanson
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the suggestions. This forum is great.

What I did was what Rob Camick suggested and it worked great (add a wrapper pane). I like the idea of using gridbaglayout for the main panel as well. The only reason I didn't was that the since the button column was fixed size and the table was the only thing that needed to resize, the borderlayout seemed to make sense. I might also have a text field under the table the reports information as the table is populated and I was thinking of making that the south component of the table frame. But I'll try it at some point and see how I like it.

To answer a couple questions that were asked- I don't know how big the table will end up being, so I allow adding/deleting rows as desired. I suspect it will be between 10 and 30 rows.

The cell renderer colors the columns. The last column is read-only and I colored it yellow. Otherwise, I am alternating the colors of even and odd rows to make reading across the table a little easier.

Thanks again for the help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic