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();
	}
	
}

hi friend,
i think you are fighting to arrange the component as per your wish
but they runing here and there :icon_cheesygrin:
Here i will give you the solution not exactly to your questions answer but an alternative to you.
you can use use the null layout for design your frame as per your wish you can control each and every part of your frame
here is the modified code for your above code.

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();
//my code
		pane.setLayout(null);//set the Layout as null
		
		labelIndex = new JLabel("Index: ");
		labelIndex.setBounds(10,10,120,20);//decide the position    
		pane.add(labelIndex);//adds into pane
		
		textFieldIndex = new JTextField("Some index...");
		textFieldIndex.setBounds(130,10,120,20);
		pane.add(textFieldIndex);
		
		labelCategory = new JLabel("Category: ");
		labelCategory.setBounds(10,40,120,20);
		pane.add(labelCategory);
		
		textFieldCategory = new JTextField("Some category...");
		textFieldCategory.setBounds(130,40,120,20);
		pane.add(textFieldCategory);
///end		   	
        frame.pack();
        frame.setVisible(true);
	}

	public static void main(String[] args) {	
		new GUI_example();
	}
	
}

here i am using no layout i.e null so i will decide the position of each and every component. so by this i can control where the component displayed what is height and width of it.

here is the syntax of it

public void setBounds(int x,int y,int width,int height)

Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.

The width or height values are automatically enlarged if either is less than the minimum size as specified by previous call to setMinimumSize.


Parameters:
x - the new x-coordinate of this component
y - the new y-coordinate of this component
width - the new width of this component
height - the new height of this component

Hope this is helpful to you but if you stick to your question then plz.
mention it in your replay.
Its all from me.
Have a good day!

hi friend,
i think you are fighting to arrange the component as per your wish
but they runing here and there :icon_cheesygrin:
Here i will give you the solution not exactly to your questions answer but an alternative to you.
you can use use the null layout for design your frame as per your wish you can control each and every part of your frame
here is the modified code for your above code.

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();
//my code
		pane.setLayout(null);//set the Layout as null
		
		labelIndex = new JLabel("Index: ");
		labelIndex.setBounds(10,10,120,20);//decide the position    
		pane.add(labelIndex);//adds into pane
		
		textFieldIndex = new JTextField("Some index...");
		textFieldIndex.setBounds(130,10,120,20);
		pane.add(textFieldIndex);
		
		labelCategory = new JLabel("Category: ");
		labelCategory.setBounds(10,40,120,20);
		pane.add(labelCategory);
		
		textFieldCategory = new JTextField("Some category...");
		textFieldCategory.setBounds(130,40,120,20);
		pane.add(textFieldCategory);
///end		   	
        frame.pack();
        frame.setVisible(true);
	}

	public static void main(String[] args) {	
		new GUI_example();
	}
	
}

here i am using no layout i.e null so i will decide the position of each and every component. so by this i can control where the component displayed what is height and width of it.

here is the syntax of it

public void setBounds(int x,int y,int width,int height)

Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.

The width or height values are automatically enlarged if either is less than the minimum size as specified by previous call to setMinimumSize.


Parameters:
x - the new x-coordinate of this component
y - the new y-coordinate of this component
width - the new width of this component
height - the new height of this component

Hope this is helpful to you but if you stick to your question then plz.
mention it in your replay.
Its all from me.
Have a good day!

Thanks for your help!! That's a good alternative too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.