Multiple JPanels with different widths inside a JFrame using GridBagLayout

I am trying to create two panels with different widths in a JFrame. I want my right pane to have the width of the left pane. I am trying to use a GridBagLayout using gridwidth = 1 for my left pane and gridwidth = 2 for my right pane. But instead of getting different widths, I get two panels with the same width. What should I change? Any help would be appreciated.

import javax.swing.*;
import java.awt.*;

public class AppFrame2 extends JFrame {

    GridBagLayout gbl;
    GridBagConstraints gbc;
    JPanel leftpanel;
    JPanel rightpanel;

    public AppFrame2() {
        this.setSize(500, 500);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.getContentPane().setBackground(Color.BLACK);

        // Settning Frame Layout
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);

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

        // Add Panels
        leftpanel = new JPanel();
        leftpanel.setBackground(Color.green);
        this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component

        rightpanel = new JPanel();
        rightpanel.setBackground(Color.red);
        this.addPanels(0, 1, 1, 2, rightpanel);
    }

    private void addPanels(int row, int col, int height, int width,
        Component com) {
        gbc.gridx = col;
        gbc.gridy = row;
        gbc.gridheight = height;
        gbc.gridwidth = width;
        gbl.setConstraints(com, gbc);
        this.getContentPane().add(com);
    }

    public static void main(String[] args) {
        new AppFrame2();
    }

}

      

+3


source to share


1 answer


Your problem is that you are not specifying the width of the columns. Adding a row gbl.columnWidths = new int[] {500/3, 500/3*2};

will set the column widths to whatever you want. I've added lines to your code here to make it work :)



import javax.swing.*;
import java.awt.*;

public class AppFrame2 extends JFrame {

    GridBagLayout gbl;
    GridBagConstraints gbc;
    JPanel leftpanel;
    JPanel rightpanel;

    public AppFrame2() {
        this.setSize(500, 500);
        this.setResizable(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.getContentPane().setBackground(Color.BLACK);

        // Settning Frame Layout
        gbl = new GridBagLayout();
        //************* New code **************//
        gbl.columnWidths = new int[] {500/3, 500/3*2};
        gbl.rowHeights = new int[] {500};
        gbl.columnWeights = new double[] {1, 1};
        gbl.rowWeights = new double[] {1};
        //************* End code **************//

        gbc = new GridBagConstraints();
        this.setLayout(gbl);

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

        // Add Panels
        leftpanel = new JPanel();
        leftpanel.setBackground(Color.green);
        this.addPanels(0, 0, 1, 1, leftpanel); // row, col, height, width component

        rightpanel = new JPanel();
        rightpanel.setBackground(Color.red);
        this.addPanels(0, 1, 1, 2, rightpanel);
    }

    private void addPanels(int row, int col, int height, int width,
                           Component com) {
        gbc.gridx = col;
        gbc.gridy = row;
        gbc.gridheight = height;
        gbc.gridwidth = width;
        gbl.setConstraints(com, gbc);
        this.getContentPane().add(com);
    }

    public static void main(String[] args) {
        new AppFrame2();
    }

}

      

+4


source







All Articles