I am adding a JPanel (buttonPanel) with a GridBagLayout to a JFrame(GUI) with a GridBagLayout. GUI is working as expected with the BOTH fill constraint while the buttonPanel isn't affected by the BOTH fill constraint. Screenshot of what is drawn
package gui_elements;
import javax.swing.*;
import calculator.Calculator;
import java.awt.*;
public class GUI extends JFrame {
private final int WIDTH = 400;
private final int HEIGHT = 600;
private Calculator calc = new Calculator();
private String text = "";
private OutputPanel outputPanel;
private ButtonPanel buttonPanel;
public GUI() {
addPanels();
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
//*****************************
public void addPanels() {
outputPanel = new OutputPanel("HELLLO!");
buttonPanel = new ButtonPanel();
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
//add and set constraints for output
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
this.add(outputPanel, c);
c.weighty = 1;
c.gridx = 0;
c.gridy = 1;
this.add(buttonPanel,c);
}
}
package gui_elements;
import javax.swing.*;
import java.awt.*;
public class ButtonPanel extends JPanel {
JButton[] buttons = new JButton[18];
public ButtonPanel() {
this.setBorder(BorderFactory.createLineBorder(Color.black));
fillButtonArr();
addButtons();
}
public void fillButtonArr() {
buttons[0] = new JButton("(");
buttons[1] = new JButton(")");
buttons[2] = new JButton(" ");
buttons[3] = new JButton("+");
buttons[4] = new JButton("1");
buttons[5] = new JButton("2");
buttons[6] = new JButton("3");
buttons[7] = new JButton("-");
buttons[8] = new JButton("4");
buttons[9] = new JButton("5");
buttons[10] = new JButton("6");
buttons[11] = new JButton("*");
buttons[12] = new JButton("7");
buttons[13] = new JButton("8");
buttons[14] = new JButton("9");
buttons[15] = new JButton("/");
buttons[16] = new JButton("Enter");
buttons[17] = new JButton("Del");
}
/**********************************
public void addButtons() {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
//operators and numbers
for (int i = 0, row = 0; i < 16; i++) {
if (i % 4 == 0) row++; //every four buttons, go to next row
c.gridy = row;
this.add(buttons[i], c);
}
//ENTER AND DELETE
c.gridy = 5;
c.gridwidth = 2;
this.add(buttons[16], c);
this.add(buttons[17], c);
}
}
The fill constraint to fill out a panel with GridBagLayout worked on the outside panel, but the didn't work on the inside panel.
In your GridBagConstraints you set the fill to both, but specify a weight for Y (vertical) only. Also when setting the second component, you are modifying the GridBagConstraints instance you passed for the first components, so both of them should hog on top of each other in grid location (0,1).
Make sure you create a new GridBagConstraints instance for each of the components.
Set the weight for both horizontal and vertical to something > 0, like
for both components.