File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problems positioning buttons in GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems positioning buttons in GridBagLayout" Watch "Problems positioning buttons in GridBagLayout" New topic

Problems positioning buttons in GridBagLayout

Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 200
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?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38487
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.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19682

And since Campbell forgot the actual moving I'll do that for him

How To Ask Questions How To Answer Questions
Wendy Gibbons

Joined: Oct 21, 2008
Posts: 1107

how big is the table? does it matter, what does the cell renerer do?
Rob Camick
Ranch Hand

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

Joined: Oct 21, 2008
Posts: 1107

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.

Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1076

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

Joined: Oct 10, 2011
Posts: 200
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.
I agree. Here's the link:
subject: Problems positioning buttons in GridBagLayout