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 4 of 4

Thread: Problem with GridBagLayout

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with GridBagLayout

    Hi guys,

    I have a problem with arranging my components in a GridBagLayout.

    The frame is 600x600. Inside it, there are two pairs of (Label,TextField). They are not where I want them to be. What I would like is to have them all at the top of the frame, one after each other. Moreover, I would like all the labels aligned together (easy) but also all the textField aligned together...

    Now, my pairs of (Label,TextField) are spread all over the panel, they aren't all at the top. The second pair is at the middle of the frame, and I would like it to be right after the first pair, just following it. And of course, the frame have to stay 600x600. I would like to be able to do that using the GridBagLayout because it will be useful later if I need to do any modification.

    I probably did all wrong with the anchors.

    Any idea how to get this result ?

    Thanks heaps!

    import java.awt.*;
    import javax.swing.*;
     
    public class GUI_example extends JFrame {
     
    	private final static long serialVersionUID = 1L;
     
    	JFrame frame;
     
    	JLabel labelIndex;
    	JLabel labelCategory;
    	JLabel labelUnicode;
     
    	JTextField textFieldIndex;
    	JTextField textFieldCategory;
    	JTextField textFieldUnicode;
     
    	public GUI_example() {
    		super();
    		createGUI();
    	}
     
    	public void createGUI() {
     
    		frame = new JFrame();
    		frame.setPreferredSize(new Dimension(600,600));
    	    frame.setTitle("Hello!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            Container pane = frame.getContentPane();
            pane.setLayout(new GridBagLayout());
     
        	GridBagConstraints c1 = new GridBagConstraints();
     
        	labelIndex = new JLabel("Index: ");
        	c1.anchor = GridBagConstraints.NORTHWEST;
        	c1.weightx = 0.5;
        	c1.weighty = 0.5;
        	c1.ipady = 10;
        	c1.gridx = 0;
        	c1.gridy = 0;
        	pane.add(labelIndex,c1);
     
        	textFieldIndex = new JTextField("Some index...");
        	c1.gridx = 1;
        	c1.gridy = 0;
        	pane.add(textFieldIndex,c1);
     
        	labelCategory = new JLabel("Category: ");
        	c1.anchor = GridBagConstraints.NORTHWEST;
        	c1.weightx = 0.5;
        	c1.weighty = 0.5;
        	c1.gridx = 0;
        	c1.gridy = 1;
        	pane.add(labelCategory,c1);
     
        	textFieldCategory = new JTextField("Some category...");
        	c1.gridx = 1;
        	c1.gridy = 1;
        	pane.add(textFieldCategory,c1);
     
            frame.pack();
            frame.setVisible(true);
    	}
     
    	public static void main(String[] args) {	
    		new GUI_example();
    	}
     
    }
    Last edited by lumpy; February 27th, 2010 at 09:27 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with GridBagLayout

    Grid bag layout will automatically space out your components. What you can do is add a "dummy" component (ex. new Jlabel("")) and tell it to fill vertically with some weighty.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with GridBagLayout

    any idea ?
    Please help, I have to submit this task early next week :/

    Thank you

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Problem with GridBagLayout

    You can use Grid Layout which automatically arranges items in a row and column symmetrically..
    Also instead you can use setBound() function to set the loaction of an item.