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?
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.
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.
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.
SCJP 1.4, OCMJEA/SCEA 5.0.
Joined: Oct 10, 2011
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.
subject: Problems positioning buttons in GridBagLayout