Java – Using GridLayout – how to set JPanel size

grid layoutjavalayout-managerswing

enter image description hereI'm using GridLayout and all my panels have the same size, how can I change their size?

I tried all functions getPreferred/Minimum/MaximumSize which makes that, and nothing.
I still want to stay with GridLayout
Any suggestions?

Best Answer

By its very nature, all components held by a GridLayout will have the same size. If you want them to be different sizes,

  1. You can use a different layout, such as a GridBagLayout or MigLayout or
  2. Use the GridLayout to hold same-sized JPanels that act as containers for other components. The inner components of course can be different sizes. For example: a chess board that holds its chess cells in a GridLayout, but that allows each cell to hold a chess piece that has varying sizes.

If this doesn't answer your question, then please clarify your question.