Hello All,
I have a problem with the
GridBagLayout when it comes to laying out the components. I tried the gridbaglayout and ended up with a lot of spaces in between components. What I actually was looking for is as shown in the second pic.
Could you please help me. I have included the code also. I tried changing the ipadx, ipady values but it did not help. I am not sure how weightx, weighty work. I never really understand them
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GridBagLayoutDemo {
private static JLabel jlbName;
private static JLabel jlbAge;
private static JLabel jlbSex;
private static JLabel jlbAddress;
private static JLabel jlbCity;
private static JLabel jlbState;
private static JTextField jtfName;
private static JTextField jtfAge;
private static JTextArea jtaAddress;
private static JTextField jtfState;
private static JTextField jtfCity;
private static JRadioButton jrbMale;
private static JRadioButton jrbFemale;
private static Container pane = null;
private static JButton jbtSubmit;
public static void addComponentsToPane(JFrame frame) {
pane = frame.getContentPane();
ButtonGroup group = new ButtonGroup();
group.add(jrbMale);
group.add(jrbFemale);
GridBagConstraints gBC = new GridBagConstraints();
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.insets = new Insets(5, 5, 0, 5);
jlbName = new JLabel("Name : ");
// gBC.weightx = 0.5;
gBC.gridx = 0;
gBC.gridy = 0;
pane.add(jlbName, gBC);
jtfName = new JTextField(20);
gBC.gridx = 1;
gBC.gridy = 0;
pane.add(jtfName, gBC);
jlbAge = new JLabel("Age : ");
gBC.gridx = 0;
gBC.gridy = 1;
pane.add(jlbAge, gBC);
jtfAge = new JTextField(5);
gBC.gridx = 1;
gBC.gridy = 1;
gBC.fill = GridBagConstraints.NONE;
pane.add(jtfAge, gBC);
gBC.fill = GridBagConstraints.HORIZONTAL;
jlbSex = new JLabel("Sex : ");
gBC.gridx = 0;
gBC.gridy = 2;
pane.add(jlbSex, gBC);
jrbMale = new JRadioButton("Male");
gBC.gridx = 1;
gBC.gridy = 2;
pane.add(jrbMale, gBC);
jrbFemale = new JRadioButton("Female");
gBC.gridx = 2;
gBC.gridy = 2;
pane.add(jrbFemale, gBC);
jlbAddress = new JLabel("Address : ");
gBC.gridx = 0;
gBC.gridy = 4;
pane.add(jlbAddress, gBC);
jtaAddress = new JTextArea(5, 10);
gBC.gridx = 1;
gBC.gridy = 4;
pane.add(jtaAddress, gBC);
jlbCity = new JLabel("City : ");
gBC.gridx = 0;
gBC.gridy = 5;
pane.add(jlbCity, gBC);
jtfCity = new JTextField(10);
gBC.gridx = 1;
gBC.gridy = 5;
pane.add(jtfCity, gBC);
jlbState = new JLabel("State : ");
gBC.gridx = 2;
gBC.gridy = 5;
pane.add(jlbState, gBC);
jtfState = new JTextField(10);
gBC.gridx = 3;
gBC.gridy = 5;
pane.add(jtfState, gBC);
jbtSubmit = new JButton("Submit");
gBC.gridx = 3;
gBC.gridy = 6;
pane.add(jbtSubmit, gBC);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayout Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
// Set up the content pane.
addComponentsToPane(frame);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Regards,
Hemanth