Results 1 to 2 of 2
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default Java Swing GridBagLayout Problem

    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

    Java Code:
    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
    Attached Thumbnails Attached Thumbnails Java Swing GridBagLayout Problem-gridbaglayout.jpg  

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    You don't need to change weight or ipad constraints for this layout.
    Works okay now.
    Java Code:
    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 GblRx {
        private JTextField jtfName;
        private JTextField jtfAge;
        private JTextArea jtaAddress;
        private JTextField jtfState;
        private JTextField jtfCity;
        private JRadioButton jrbMale;
        private JRadioButton jrbFemale;
        private JButton jbtSubmit;
    
        public void addComponentsToPane(JFrame frame) {
            Container 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);
    
            JLabel jlbName = new JLabel("Name : ");
            gBC.gridx = 0;
            gBC.gridy = 0;
            pane.add(jlbName, gBC);
    
            jtfName = new JTextField(20);
            gBC.gridx = 1;
            gBC.gridy = 0;
            gBC.gridwidth = 2;
            pane.add(jtfName, gBC);
    
            JLabel jlbAge = new JLabel("Age : ");
            gBC.gridx = 0;
            gBC.gridy = 1;
            gBC.gridwidth = 1;
            pane.add(jlbAge, gBC);
    
            jtfAge = new JTextField(5);
            gBC.gridx = 1;
            gBC.gridy = 1;
            gBC.fill = GridBagConstraints.NONE;
            gBC.anchor = GridBagConstraints.WEST;
            gBC.gridwidth = 2;
            pane.add(jtfAge, gBC);
    
            gBC.fill = GridBagConstraints.HORIZONTAL;
    
            JLabel jlbSex = new JLabel("Sex : ");
            gBC.gridx = 0;
            gBC.gridy = 2;
            gBC.gridwidth = 1;
            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);
    
            JLabel jlbAddress = new JLabel("Address : ");
            gBC.gridx = 0;
            gBC.gridy = 3;//4;
            pane.add(jlbAddress, gBC);
    
            jtaAddress = new JTextArea(5, 10);
            gBC.gridx = 1;
            gBC.gridy = 3;//4;
            gBC.gridwidth = 2;
            pane.add(jtaAddress, gBC);
    
            JLabel jlbCity = new JLabel("City : ");
            gBC.gridx = 0;
            gBC.gridy = 4;//5;
            gBC.gridwidth = 1;
            pane.add(jlbCity, gBC);
    
            jtfCity = new JTextField(10);
            gBC.gridx = 1;
            gBC.gridy = 4;//5;
            gBC.gridwidth = 2;
            pane.add(jtfCity, gBC);
    
            gBC.gridwidth = 1;
    
            JLabel jlbState = new JLabel("State : ");
            gBC.gridx = 3;//2;
            gBC.gridy = 4;//5;
            gBC.insets.right = 0;
            pane.add(jlbState, gBC);
    
            gBC.insets.right = 5;
    
            jtfState = new JTextField(10);
            gBC.gridx = 4;//3;
            gBC.gridy = 4;//5;
            pane.add(jtfState, gBC);
    
            jbtSubmit = new JButton("Submit");
            gBC.gridx = 4;//3;
            gBC.gridy = 5;//6;
            pane.add(jbtSubmit, gBC);
        }
    
        public static void main(String[] args) {
            GblRx test = new GblRx();
            JFrame frame = new JFrame("GridBagLayout Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new GridBagLayout());
            test.addComponentsToPane(frame);
            frame.pack();
            frame.setVisible(true);
        }
    }

Similar Threads

  1. SWT & Swing Bridge problem
    By roshithmca in forum AWT / Swing
    Replies: 0
    Last Post: 03-26-2008, 02:23 PM
  2. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:16 AM
  3. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 09:03 PM
  4. jdbc/ swing code problem
    By sami in forum Advanced Java
    Replies: 1
    Last Post: 08-13-2007, 03:26 PM
  5. Problem with GridBagLayout
    By Daniel in forum SWT / JFace
    Replies: 2
    Last Post: 07-01-2007, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •