roeygol
roeygol

Reputation: 5028

Using GridLayout - how to set JPanel size?

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?

Upvotes: 2

Views: 12310

Answers (2)

roeygol
roeygol

Reputation: 5028

So I managed to split all the part above to my first JPanel which located to NORTH.
Then, the JPanel with the button I did the same to be located SOUTH.
So the scrolled JLabel in now located in the CENTER which allows him to be flexible.

    JPanel mainPanel1 = new JPanel(new GridLayout(6,1));
    mainPanel1.add(titleLabel);
    mainPanel1.add(participantPanel);
    mainPanel1.add(swimPanel);
    mainPanel1.add(ridePanel);
    mainPanel1.add(runPanel);
    mainPanel1.add(categoriesPanel);

    JPanel mainPanel2 = new JPanel(new GridLayout(1,1));        
    mainPanel2.add(listPanel);

    JPanel mainPanel3 = new JPanel(new GridLayout(1,1));
    mainPanel3.add(buttonsPanel);

    this.getContentPane().add(mainPanel1, BorderLayout.NORTH);
    this.getContentPane().add(mainPanel2, BorderLayout.CENTER);
    this.getContentPane().add(mainPanel3, BorderLayout.SOUTH);

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 7

Related Questions