954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GridBagLayout() need help please

Hi
This is a code for placing a few labels.
Now, I have a problem with my grid.
I want the 0,0 point in the upper left corner but at the moment it is around the middle.
Can someone help me please?

import javax.swing.*;
import java.awt.*;
public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame("test");
		frame.setSize(500, 500);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setLocation(0, 0);
		panel.setBackground(Color.pink);
		frame.add(panel);
		frame.getContentPane().add(panel,BorderLayout.PAGE_START);
		//
		GridBagConstraints c = new GridBagConstraints();
		//JLabel lbl0 = new JLabel("0");
		//c.gridx = -1;
		//c.gridy = -1;
		//panel.add(lbl0,c);
		JLabel lbl1 = new JLabel("1");
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(10, 10, 10, 10);
		panel.add(lbl1,c);
		JLabel lbl2 = new JLabel("2");
		c.gridx = 1;
		c.gridy = 1;
		panel.add(lbl2,c);
		JLabel lbl3 = new JLabel("3"); 
		c.gridx = 2;
		c.gridy = 2;
		panel.add(lbl3,c);
		JLabel lbl4 = new JLabel("4"); 
		c.gridx = 3;
		c.gridy = 3;
		panel.add(lbl4,c);
		JLabel lbl5 = new JLabel("5"); 
		c.gridx = 4;
		c.gridy = 4;
		panel.add(lbl5,c);
	}

}
superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

Take a look at the anchor property.

Edit: You'll also need to look at the weightx property. You can read about it in the general notes for the GridBagLayout class.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Take a look at the anchor property.

Edit: You'll also need to look at the weightx property. You can read about it in the general notes for the GridBagLayout class.


And weighty , of course (if he wants the upperleft, otherwise he'll just get the left. ;-)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I'm going to try it

superjj
Light Poster
32 posts since Nov 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: