Arkhan6 Arkhan6 - 1 month ago 7
Java Question

How to expand Jpanels into GridBagLayout

My Question is how to expand my 3 JPanels into my GrigBadLayout.
I added 2 photo, the first sample and displays the result and the second as I would like it to be.

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JPanel;


public class ListMembersView extends JPanel{
JPanel headerPanel;
JPanel containerPanel;
JPanel footerPanel;

public ListMembersView(){

initComponents();
initLayout();
}

private void initComponents(){

this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255),5));

headerPanel = new JPanel();
headerPanel.setBackground(Color.red);
containerPanel = new JPanel();
containerPanel.setBackground(Color.yellow);
footerPanel = new JPanel();
footerPanel.setBackground(Color.blue);
}

private void initLayout(){
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
/*Not expand JPanel?*/
c.fill = GridBagConstraints.HORIZONTAL;

c.gridx=0; c.gridy=0;
this.add(headerPanel,c);

c.gridx=0; c.gridy=1;
c.gridwidth = 3;
this.add(containerPanel,c);

c.gridx=0; c.gridy=2;
this.add(footerPanel,c);

}
}


My result is:

enter image description here

I would like be that way:

enter image description here

Answer

You need to set columnWeights = {1} and rowWeights = {1, 1,1} and fill to BOTH like:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class ListMembersView extends JPanel{
    JPanel headerPanel;
    JPanel containerPanel;
    JPanel footerPanel;

    public ListMembersView(){

        initComponents();
        initLayout();
    }

private void initComponents(){

    this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255),5));

    headerPanel = new JPanel();
    headerPanel.setBackground(Color.red);
    containerPanel = new JPanel();
    containerPanel.setBackground(Color.yellow);
    footerPanel = new JPanel();
    footerPanel.setBackground(Color.blue);
}    

private void initLayout(){    
    GridBagLayout gridBagLayout = new GridBagLayout();
    this.setLayout(gridBagLayout);

    gridBagLayout.columnWeights = new double[] {1};
    gridBagLayout.rowWeights = new double[] {1, 1, 1};
    GridBagConstraints c = new GridBagConstraints();
    /*Not expand JPanel?*/
    c.fill = GridBagConstraints.BOTH;

    c.gridx=0; c.gridy=0; 
    this.add(headerPanel,c);

    c.gridx=0; c.gridy=1; 
    c.gridwidth = 3;
    this.add(containerPanel,c);

    c.gridx=0; c.gridy=2; 
    this.add(footerPanel,c);

}

public static void main(String[] args) {
    JFrame frame = new JFrame("test");
    frame.setLayout(new BorderLayout());
    frame.add(new ListMembersView());
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

The fill=BOTH teels, that panels should be extended in both directions.

the weights tells how to divide the space beetwen the rows or columns proportionally to the overall sum of their weights.