Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: GridBagLayout help

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GridBagLayout help

    Hello,

    I am currently creating a GUI using GridBagLayout and I am having an issue with putting JLabels next to JPanels. I'm pairing JLabels with JTextFields in the same row as titles for data. For some reason the JLabels seem to be much bigger than the JTextFields even though I used the same Weight. Also there seems to be extra padding to the right of each JLabel because there is a lot of blank space before the JTextField begins. Ideally I would like the JTextField and JLabels to each take up half of the space in the panel.

    The panel I'm referring to is the far right panel (helper date, helper time, etc.) Here is the code:

    package NewGui;
     
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.SystemColor;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.EtchedBorder;
     
    public class NewGui extends JFrame {
     
    	JPanel mainGridBagPanel = new JPanel(new GridBagLayout());
    	JPanel topButtonPanel = new JPanel(new GridBagLayout());
     
    	JPanel helperAddressPanel = new JPanel(new GridBagLayout());
    	JPanel helperNamePanel = new JPanel(new GridBagLayout());
    	JPanel filerInfoPanel = new JPanel(new GridBagLayout());
    	JPanel dupPanel = new JPanel(new GridBagLayout());
     
    	JButton helpInformationButton = new JButton();
    	JButton returnPreviousButton = new JButton();
    	JButton viewErrorLogButton = new JButton();
    	JLabel listLabel = new JLabel();
     
    	JLabel helperAddressLabel = new JLabel();
    	JTextField helperAddressOne = new JTextField();
    	JTextField helperAddressTwo = new JTextField();
    	JTextField coLine = new JTextField();
    	JLabel codeLabel = new JLabel();
    	JTextField code = new JTextField();
    	JTextField phoneNbr = new JTextField();
     
    	JLabel helperNameLabel = new JLabel();
    	JTextField addressOne = new JTextField();
    	JTextField addressTwo = new JTextField();
    	JTextField city = new JTextField();
    	JTextField prov = new JTextField();
    	JTextField cntry = new JTextField();
    	JTextField postalCd = new JTextField();
    	JLabel dateLabel = new JLabel();
    	JTextField date = new JTextField();
     
    	JLabel helperDateLabel = new JLabel();
    	JTextField helperDate = new JTextField();
    	JLabel timeLabel = new JLabel();
    	JTextField time = new JTextField();
    	JLabel locationLabel = new JLabel();
    	JTextField location = new JTextField();
    	JLabel descriptionLabel = new JLabel();
    	JTextField description = new JTextField();
    	JLabel destinationLabel = new JLabel();
    	JTextField destination = new JTextField();
    	JLabel typeLabel = new JLabel();
    	JTextField type = new JTextField();
    	JLabel durationLabel = new JLabel();
    	JTextField duration = new JTextField();
     
     
     
    	public NewGui()
    	{
    		setPanelInfo();
    		setLabels();
    		setValues();
     
    		this.getContentPane().add(mainGridBagPanel);
    		GridBagConstraints gb = new GridBagConstraints();
    		gb.fill = GridBagConstraints.BOTH;
    		gb.gridx = 0;
    		gb.gridy = 0;
    		gb.weightx = 0.5;
    		gb.weighty = 0.5;
    		gb.gridwidth = 3;
    		gb.insets = new Insets(5,5,0,5); 
    		mainGridBagPanel.add(topButtonPanel, gb);
    		gb.gridy = 1;
    		gb.gridwidth = 1;
    		gb.insets = new Insets(5,5,5,0); 
    		mainGridBagPanel.add(helperAddressPanel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(5,5,5,0); 
    		mainGridBagPanel.add(helperNamePanel, gb);
    		gb.gridx = 2;
    		gb.insets = new Insets(5,5,5,5); 
    		mainGridBagPanel.add(filerInfoPanel, gb);
    //		gb.gridx = 3;
    //		gb.insets = new Insets(10,10,10,10); 
    //		mainGridBagPanel.add(dupPanel, gb);
     
    		gb.gridx = 0;
    		gb.gridy = 0;
    		gb.insets = new Insets(5,5,5,0);  
    		topButtonPanel.add(helpInformationButton, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(5,5,5,0);  
    		topButtonPanel.add(listLabel, gb);
    		gb.gridx = 2;
    		gb.insets = new Insets(5,5,5,0);  
    		topButtonPanel.add(returnPreviousButton, gb);
    		gb.gridx = 3;
    		gb.insets = new Insets(5,5,5,5);  
    		topButtonPanel.add(viewErrorLogButton, gb);
     
    		gb.gridx = 0;
    		gb.gridy = 0;
    		gb.insets = new Insets(5,5,5,0); 
    		helperAddressPanel.add(helperAddressLabel, gb);
    		gb.gridy = 1;
    		gb.insets = new Insets(0,0,0,0); 
    		helperAddressPanel.add(helperAddressOne, gb);
    		gb.gridy = 2;
    		helperAddressPanel.add(helperAddressTwo, gb);
    		gb.gridy = 3;
    		helperAddressPanel.add(coLine, gb);
    		gb.gridy = 4;
    		helperAddressPanel.add(codeLabel, gb);
    		gb.gridy = 5;
    		helperAddressPanel.add(code, gb);
    		gb.gridy = 6;
    		helperAddressPanel.add(phoneNbr, gb);
     
    		gb.gridx = 0;
    		gb.gridy = 0;
    		gb.gridwidth = 3;
    		gb.insets = new Insets(5,5,5,0); 
    		helperNamePanel.add(helperNameLabel, gb);
    		gb.gridy = 1;
    		gb.insets = new Insets(0,0,0,0);
    		helperNamePanel.add(addressOne, gb);
    		gb.gridy = 2;
    		helperNamePanel.add(addressTwo, gb);
    		gb.gridy = 3;
    		helperNamePanel.add(city, gb);
    		gb.gridy = 4;
    		gb.gridwidth = 1;
    		helperNamePanel.add(prov, gb);
    		gb.gridx = 1;
    		helperNamePanel.add(cntry, gb);
    		gb.gridx = 2;
    		helperNamePanel.add(postalCd, gb);
    		gb.gridx = 0;
    		gb.gridy = 5;
    		gb.gridwidth = 3;
    		helperNamePanel.add(dateLabel, gb);
    		gb.gridy = 6;
    		helperNamePanel.add(date, gb);
     
    		gb.gridx = 0;
    		gb.gridy = 0;
    		gb.gridwidth = 1;
    		gb.insets = new Insets(5,5,5,0);
    		filerInfoPanel.add(helperDateLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(helperDate, gb);
    		gb.gridx = 0;
    		gb.gridy = 1;
    		gb.insets = new Insets(0,5,0,0);
    		filerInfoPanel.add(timeLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(time, gb);
    		gb.gridx = 0;
    		gb.gridy = 2;
    		gb.insets = new Insets(0,5,0,0);
    		filerInfoPanel.add(locationLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(location, gb);
    		gb.gridx = 0;
    		gb.gridy = 3;
    		gb.insets = new Insets(0,5,0,0);
    		filerInfoPanel.add(descriptionLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(description, gb);
    		gb.gridx = 0;
    		gb.gridy = 4;
    		gb.insets = new Insets(0,5,0,0);
    		filerInfoPanel.add(destinationLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(destination, gb);
    		gb.gridx = 0;
    		gb.gridy = 5;
    		gb.insets = new Insets(0,5,0,0);
    		filerInfoPanel.add(typeLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(type, gb);
    		gb.gridx = 0;
    		gb.gridy = 6;
    		gb.insets = new Insets(0,5,0,0);
    		filerInfoPanel.add(durationLabel, gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		filerInfoPanel.add(duration, gb);
     
     
    	}
     
    	public void setPanelInfo()
    	{
    		topButtonPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    		topButtonPanel.setBackground(SystemColor.activeCaption);
     
    		helperAddressPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    		helperAddressPanel.setBackground(SystemColor.activeCaption);
     
    		helperNamePanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    		helperNamePanel.setBackground(SystemColor.activeCaption);
     
    		filerInfoPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    		filerInfoPanel.setBackground(SystemColor.activeCaption);
     
    	}
     
    	public void setLabels()
    	{
     
    		helpInformationButton.setText("Help with information");
    		returnPreviousButton.setText("Return to previous screen");
    		viewErrorLogButton.setText("View error log");
    		listLabel.setText("List");
    		listLabel.setHorizontalAlignment(SwingConstants.CENTER);
    		listLabel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.BLACK));
    		listLabel.setOpaque(true);
    		listLabel.setBackground(SystemColor.control);
     
    		helperAddressLabel.setText("Helper Address");
    		codeLabel.setText("Code");
     
    		helperNameLabel.setText("Helper Name");
    		dateLabel.setText("Data");
     
    		helperDateLabel.setText("Helper Date");
    		timeLabel.setText("Time");
    		locationLabel.setText("Location");
    		descriptionLabel.setText("Destination");
    		destinationLabel.setText("Description");
    		typeLabel.setText("Type");
    		durationLabel.setText("Duration");
     
     
    	}
     
    	public void setValues()
    	{
     
     
    	}
     
     
     
    	public static void main (String args[])
    	{
     
    		try {
    			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    				if ("Nimbus".equals(info.getName())) {
    					UIManager.setLookAndFeel(info.getClassName());
    					break;
    				}
    			}
    		} catch (Exception e) {
    			// If Nimbus is not available, you can set the GUI to another look and feel.
    		}
     
    		NewGui gui = new NewGui();
    		gui.pack();
    		gui.setVisible(true);
     
     
     
    	}
     
    }


    Any help is greatly appreciated.

    Thanks.
    Last edited by curmudgeon; June 15th, 2013 at 10:22 PM. Reason: code tags added.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: GridBagLayout help

    sometimes it's useful to specify the column size of your JTextFields. For instance,

       JTextField helperDate = new JTextField(7);

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GridBagLayout help

    Thanks for the tip. It doesn't work how I want though because when you resize the Frame the Textfield size behaves unusually. It actually shrinks back to the original size (before I had set a column size) if I just resize the Frame to be a smidgen smaller.

    I actually did a test to show the blank space between a JLabel and a JTetField using GridBagLayout and GridLayout.

    If you run the code below you will see there is a space between the JLabel and JTextfield. If you uncomment the 3 gridlayout lines, comment the GridBagLayout lines, and rerun the code you will see there is no gap using GridLayout. Does anyone know why this is? And if there is a way to remove the space between a JLabel and JTextField in GridBagLayout.

    Here is the code:
    package NewGui;
     
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.Insets;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class TestGui extends JFrame {
     
    	JPanel mainPane = new JPanel(new GridBagLayout());
     
    	public TestGui()
    	{
    		this.getContentPane().setLayout(new GridBagLayout());
    		GridBagConstraints gb = new GridBagConstraints();
    		gb.gridx = 0;
    		gb.gridy = 0;
    		gb.weightx = 0.1;
    		gb.weighty = 0.1;
    		gb.insets = new Insets(0,0,0,0);
    		gb.fill = GridBagConstraints.BOTH;
    		this.getContentPane().add(new JLabel("Name of Person"), gb);
    		gb.gridx = 1;
    		gb.insets = new Insets(0,0,0,0);
    		this.getContentPane().add(new JTextField(), gb);
     
    //		this.getContentPane().setLayout(new GridLayout(1,2));
    //		this.getContentPane().add(new JLabel("Name of Person"));
    //		this.getContentPane().add(new JTextField());
    // 
    		this.setTitle("program");
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    	}
     
    	public static void main(String args[])
    	{
    		new TestGui();
     
    	}
     
    }

Similar Threads

  1. Help with GridBagLayout
    By mikejr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 20th, 2012, 07:52 AM
  2. GridBagLayout can't seem to get it right.
    By ToyYoda in forum AWT / Java Swing
    Replies: 1
    Last Post: August 24th, 2011, 02:19 AM
  3. GridBagLayout help
    By mjpam in forum AWT / Java Swing
    Replies: 1
    Last Post: May 8th, 2011, 10:51 AM
  4. GridBagLayout help
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: August 19th, 2010, 06:35 PM
  5. [SOLVED] Problem with GridBagLayout
    By lumpy in forum AWT / Java Swing
    Replies: 3
    Last Post: February 28th, 2010, 12:58 PM