Search code examples
javaswinglayout-managergridbaglayout

Java, GridBagLayout. How add components right to other component


i'm learning JSwing and i discovered the GridBagLayout.

I'm trying to create a simple calculator, i did it with adding multiple JPanel setting each preferedSize but when i resize the window frame the panels won't resize too. Then i found out the GridBagLayout.

But this i what i get: Wrong calculator with GridBagLayout

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

public class Calc extends JFrame {

private final int  WIDTH = 300;
private final int  HEIGHT = 450;

public Calc(){

    setSize(WIDTH, HEIGHT);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());




    mainPanel.add(createButtons(), BorderLayout.SOUTH);


    add(mainPanel);


}

private  JPanel createButtons(){

    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();

    panel.setLayout(layout);


    GridBagConstraints g = new GridBagConstraints();

    g.gridx = 0;
    g.gridy = 0;

    for(int i = 0; i < 9; i++){

        panel.add(new JButton(""+i), g);
        g.gridx++;
        if(g.gridx == 3) {
            g.gridx = 0;
            g.gridy++;
        }

    }


    return panel;



}


public static void main(String... args){

    Calc calc = new Calc();
    calc.setVisible(true);

}

}

it should be something like this: Right calculator

i tried:

  • to set an anchor... but it doesn't work,
  • to create multiple JPanel(one with GridLayout) but doesn't work

if you don't want to spoon code, it's okay.. but from where should i start?

Edit: I figure out how to arrange the buttons... but i can't set the header to fill all the x-axis: Code:

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

public class ButtonPanel extends JPanel {

JPanel top;
JPanel left;
JPanel right;


private class CButton extends JButton{

    private Operation operation;

    public CButton(){

    }



}

public ButtonPanel(){

    initComponent();
    initLayout();
}

private void initLayout() {

    GridBagLayout layout = new GridBagLayout();
    this.setLayout(layout);

    layout.columnWeights = new double[] {3,1};
    layout.rowWeights = new double[] {1, 1};

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;

    c.weightx = 5;
    this.add(top, c);
    c.gridy++;
    c.weighty=1;
    this.add(left, c);
    c.gridx++;
    this.add(right, c);




}

private void initComponent() {

    top = new JPanel();

    top.setLayout(new GridLayout(1, 3));

    for(int i = 0; i < 3; i++){
        top.add(new JButton("bbb"));
    }



    left = new JPanel();

    left.setLayout(new GridLayout(3,3));
    for(int i = 0; i < 9; i++){

        left.add(new JButton(""+i));

    }

    right = new JPanel();
    right.setLayout(new GridLayout(3,1));
    for(int i = 0; i < 3; i++){
        JButton btn = new JButton("aa");
        right.add(btn);

    }

}

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

}

It should be: Image


Solution

  • You can probably do everything within a single panel, having some buttons that span over multiple columns.

    So I give you a different example to layout buttons using a single GridBagLayout, here you can define your button arrangement as an array of values, check if it could be a good starting point for your project.

    package test;
    
    import static test.Calculator.Buttons.*;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Calculator extends JPanel {
        //Here define all possible buttons with labels
        public enum Buttons {
            PERCENT("%"), CE("CE"), CLEAR("C"),
            ONE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"), NINE("9"), ZERO("0"),
            ADD("+"), SUB("-"), MULT("x"), DIV("/"), RESULT("="), DECPOINT(".");
            
            protected String text;
            
            Buttons(String txt) {
                this.text=txt;
            }
            
            public String getText() {
                return text;
            }
        };
        
        //This array contains your keypad layout, contiguous repeated elements will span across multiple columns (e.g. ZERO). 
        protected Buttons[][] keyPad = {
            {PERCENT, CE, CLEAR, DIV},
            {SEVEN, EIGHT, NINE, MULT},
            {FOUR, FIVE, SIX, ADD},
            {ONE, TWO, THREE, SUB},
            {ZERO, ZERO, DECPOINT, RESULT}
        };
        
        Map<JButton, Buttons> sourceMap=new HashMap<>();
        
        ActionListener padListener=new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onKeyPadPressed(sourceMap.get(e.getSource()));
            }
        };
        
        public Calculator() {
            setLayout(new GridBagLayout());
            GridBagConstraints c=new GridBagConstraints();
            c.weightx=1.0;
            c.weighty=1.0;
            c.fill=GridBagConstraints.BOTH;
            
            for (int y=0;y<keyPad.length;y++) {
                for (int x=0;x<keyPad[y].length;) {
                    Buttons b=keyPad[y][x];
                    if (b==null) {
                        continue;
                    }
                    JButton btn=new JButton(b.getText());
                    c.gridx=x;
                    c.gridy=y;
    
                    c.gridwidth=0;
                    while(x<keyPad[y].length&&keyPad[y][x]==b) {
                        c.gridwidth++;
                        x++;
                    }
                    add(btn,c);
                    
                    sourceMap.put(btn,b);
                    btn.addActionListener(padListener);
                }
            }
            
        }
        
        //Callback method, whenever a button is clicked you get the associated enum value here
        protected void onKeyPadPressed(Buttons b) {
            System.out.println("Pressed "+b);
            switch (b) {
    //          case ZERO:
    //          .... here your logic
            }
        }
        
        public static void main(String[] args) {
            JFrame frame=new JFrame("Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new Calculator());
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    The code above produces this result, but it's really easy to add/remove buttons and change the layout.

    enter image description here