1

Im setting the content pane of a JFrame to a panel that I've created. This panel makes use of the Grid Bag Layout for its subsequent panels. I set this layout like so:

private void initPanel() {

    setLayout(new GridBagLayout());
    constraints = new GridBagConstraints();
    populateGUI();
}

This causes the "game" to like like this:

enter image description here

What I'm actually trying to do is to expand those panels (the tiny squares) out. The dimensions of the JFrame are 800 x 500, so each panel should occupy 100 x 100 making a total of 40 panels. The problem is that the panels are just too small as they are.

I populate the content pane of the JFrame (replaced by own panel - marked in red) with this code:

private void populateGUI() {

    // Fill the frame with panels
    for (int rowPos = 0; rowPos < numColumns; rowPos++) {

        for (int colPos = 0; colPos < numRows; colPos++) {

            constraints.gridx = rowPos;
            constraints.gridy = colPos;

            // Add component
            add(new GameDummyPanel(gameUtil.generateRandomColour(),
                    panelWidth, panelHeight), constraints);
        }
    }
}

Note - those are the only constraints that I implement. In the GameDummyPanel class i set the size using the following:

    public GameDummyPanel( Color panelColor, int panelWidth, int panelHeight )
{
    dummyPanelWidth = panelWidth / 8;
    dummyPanelHeight = panelHeight / 5;

    setBackground( panelColor );
    setSize( new Dimension( dummyPanelWidth, dummyPanelHeight ) );
}

so why isn't this being enforced? Its not like I'm calling pack on the panel and on the panel that acts as the content pane of the JFrame.

Thanks

Update

Having examined and implemented the answers what needed to be done - should you not want to read below - is to set the fill property of the contraints to BOTH and remove all size based properties from the panel class. This means my constraints look like this:

    constraints = new GridBagConstraints();
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;

Minus the actually unique positioning for each of the panels set up gridx and gridy. the JPanel implementation could then actually be done away with and have the JPanel just created on the fly.

Katana24
  • 8,706
  • 19
  • 76
  • 118
  • this post might help: http://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone – Dan O May 07 '14 at 16:24

3 Answers3

2

Set the following values to the panels:

constraints.weightx = 1.0;
constraints.weighty = 1.0;

From http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.

Also you should not set the size of the panels manually, let the GridBagLayout do its work, just configure the constraints to your liking, for the simplest layout you should at least consider the following variables:

gridx, gridy
fill
anchor
weightx, weighty

all of them explained in the previous link.

DSquare
  • 2,458
  • 17
  • 19
  • 2
    Without using `constraints.fill=GridBagConstraints.BOTH;` the output will be different. – Braj May 07 '14 at 16:40
  • @Braj That is correct, I was just giving the core offender of his problem, from that point on he should custom his layout to his needs. since as far as we know, a GridLayout would be more appropiate, as you pointed out. Also he is forcing the size of the panels so he might get something good enough just with it. – DSquare May 07 '14 at 16:49
  • a combination of what you said and setting the fill property did the trick. Without the fill property the panels will be rather small. – Katana24 May 07 '14 at 18:01
2

Try with some more properties of GridBagConstraints.

 constraints.weightx=1.0;
 constraints.weighty=1.0;
 constraints.fill=GridBagConstraints.BOTH;

OR

Use GridLayout instead of GridBagLayout in this case.

private void initPanel() {
    setLayout(new GridLayout(numRows, numColumns));
    populateGUI();
}

enter image description here

Braj
  • 46,415
  • 5
  • 60
  • 76
1
  • Don't call setSize() on the panel.

  • Do override getPreferredSize() of the panel. GridBagLayout respects preferred sizes. If you don't override, it will have that effect your experiencing.

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(SIZE, SIZE);
    }
    
  • Do call frame.pack() so the preferredSize is respected (Don't set the frame size)

  • Do see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for the answer - are your recommendations above best practices? If so where can I read about them? – Katana24 May 08 '14 at 07:44