Is it possible to completely emulate the behavior of a GridLayout with the GridBagLayout manager?
Basically, I have a 8x8 grid in which each cell should have the same width and height. The GridLayout automatically did this. But I want to add another row and column to the grid which size is not the same as the other ones. That row/column should take up all the remaining space that might be left over (because the available size couldn't be equally distributed into 8 cells). Is that even possible, or do I – again – have to use a different layout manager?
edit
Here is a simple graphic of what I want to achieve, simplified to just 4 cells:
The colored cells are the ones I added to the actual grid (gray) which has cells with the same height and width x
. So the grid's height and width is 4*x
. I now want the additional cells to have the necessary width/height (minimumSize) plus the rest of the available width/height from the full size.
If the whole panel's size is changed, the gray grid cells should again take up as much as space as possible.
All values are set in the
GridBagConstraints
.If you want your 8x8 grid fixed, you can set the
ipadx
andipady
values.Then set the
weightx
andweighty
values of your new row/column to1.0
and setfill
toFULL
.Your extra row/column will expand with the space that is left over.
If you want the 8x8 grid to expand as well, that is more complicated. You can adjust the
ipadx
andipady
values in addition to theweightx
andweighty
values. Or make a panel for the 8x8 grid and use aGridLayout
in there. Use yourGridBagLayout
on the panel and the additional row/column.Is there a reason you're not doing this in a JTable? Seems like just the sort of thing it's made for. With the appropriate cell renderer you can put anything you want in the cells.
http://www.java2s.com/Code/Java/Swing-Components/TableRowHeaderExample.htm
set
weightx
andweighty
ofGridBagConstraints
of the fixed cells to0
and thefill
toNONE
. For the floating cells setfill
toBOTH
, for the floating cells that should expand only horizontally setweightx
to1
and for the vertically expanding ones setweighty
to1
.The cells only expand if they have any content, so you need to fill it with something. I chose
JLabel
s and set fixed dimensions for the labels in the fixed cells. On resize you need to recalculate the dimensions and callinvalidate()
to recalculate the layout.Here is an example for a
w
xh
grid:If you're not tied to the GridBagLayout necessarily, you should look into the MigLayout library. You can do something along the lines of:
After trying many different things with the built-in layout managers, I decided to create a custom layout manager for this problem as well. I didn't do it yet, as I didn't have the time to continue with this project, but when I have it done, I'll make sure to post the layout manager code here, so that anyone interested in a similar solution can use it.
edit
didxga reminded me in the comments that I wanted to post my solution. However, after digging out the project from back then and looking at it, I actually cannot post my solution because it turns out that I never got to creating it!
It was a uni project that finished official mid September 2010. We actually wanted to continue working on it afterwards, which is probably why I said that I would post it (as that was one thing I wanted to improve), but we never really got around doing it – sadly. Instead I simply left out those extra column and row (which was meant as a label for the rows/columns btw).
So yeah, I’m terribly sorry that I cannot post a layout that does what I initially wanted… :( Maybe if there are enough requesting such a layout, I would create it, but as of now, I’m not really willing to dive into Java layouting again ;)