Basicly i’ve got this setup
| JFrame with BorderLayout |
| -------------------------- |
| JPanel with GridBagLayout |
| JPanel with NullLayout |
The JFrame is resizable. The JPanel with GridBagLayout contains itselt gain JPanels and looks basicly like this
|Obj|Obj|Obj|
|Obj|Obj|Obj|
|Obj|Obj|Obj|
The Grid should be quadratic.
If I resize the window, the components will be adjusted automaticly and because of this, the Grid looses it’s qudratic dimension and becomes an rectangel.
I can prevent this by writing my own layout manager. So my question to you: is there another possibility to enforce a quadratic grid?
Instead of using
GridBagLayout, consider using MigLayout.You can also use a
nulllayout which sets width and height of each component as the width or height divided by either number of rows or columns (depending on how you want to go about it), and set the initialxandyto values according to this:(total width % number of columns) / 2and the others to simply
initialX + cellWidth * (col - 1)andinitialY + cellWidth * (row - 1).Put this code into your
componentResized(ComponentEvent e){...}method of your middle
JPanel‘sComponentListener, and cycle through your cells (panels) setting their bounds. This might be a slow / manual way but you’ll have complete control of exactly how you want them to resize.