I have this GUI
I have used GridBagLayout for it but I do not know why there is a huge spacing between the Plain Bread checkbox and its corresponding label.
And also, I have tried to increase the spacing along the x-axis only for the button but it has increased along all the components in the x-axis despite resetting the insets (but the spacing between the checkbox between Plain Bread and the label existed before setting the insets for the button).
Here are the codes:
public Bakery(){
super("Bakery Bread Option");
setLayout(new FlowLayout());
panel.setLayout(new GridBagLayout());
add(panel);
GridBagConstraints gbc= new GridBagConstraints();
gbc.insets= new Insets(4,4,4,4);
gbc.gridx=0;
gbc.gridy=0;
panel.add(option_Title, gbc);
gbc.gridx=0;
gbc.gridy=1;
panel.add(check_Plain, gbc);
gbc.gridx=0;
gbc.gridy=2;
panel.add(qty_Plain, gbc);
gbc.gridx=0;
gbc.gridy=3;
panel.add(qty_Brown, gbc);
gbc.insets= new Insets(0,175,0,0);
gbc.gridx=0;
gbc.gridy=4;
panel.add(calc_Btn, gbc);
gbc.insets= new Insets(4,4,4,4);
gbc.gridx=1;
gbc.gridy=1;
panel.add(plain_Bread, gbc);
gbc.gridx=1;
gbc.gridy=2;
panel.add(qtyPlainText, gbc);
gbc.gridx=1;
gbc.gridy=3;
panel.add(qtyBrownText, gbc);
gbc.gridx=2;
gbc.gridy=1;
panel.add(check_Brown, gbc);
gbc.gridx=3;
gbc.gridy=1;
panel.add(brown_Bread, gbc);
calc_Amt calc_Handler= new calc_Amt();
calc_Btn.addActionListener(calc_Handler);
}
EDIT:
This is the GUI I intend to achieve:
there is a huge spacing between the Plain Bread checkbox and its corresponding label.
Because the components are added to different columns.
The width of a column is determined by the width of the largest component in the column. So the text component determines the width of the column, not the label.
If you want the label/check box to be together, you can create a JPanel and add the two components to the panel. Then add the panel to the gridbaglayout.
Or you could have the two text fields use a columnWidth of 2, which means they will span two columns.