GridBag - layout issue

I am really strugglingwith this grid bag, i would like for the label in my grid at

gridx and gridy 0 to be at the top left hand corner of my window but for some reason 0,0 seems to set my label in the center of the window. heres a snippet of the code maybe it will help:

mainpanel.setLayout(new GridBagLayout());

GridBagConstraints labellay= new GridBagConstraints();

//Labels

labellay.anchor=GridBagConstraints.EAST;

mainpanel.add(lengthlabel,labellay);

labellay.gridx=0;

labellay.gridy=0;

//labellay.gridwidth=2;

labellay.anchor=GridBagConstraints.WEST;

labellay.weightx = 0.5;

labellay.gridx = 1;

labellay.gridy = 0;

mainpanel.add(lengthtext, labellay);

when i say window i mean my swing window

[794 byte] By [regularjohna] at [2007-10-3 5:15:16]
# 1

I generally recommend using another LayoutManager.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 23:21:52 > top of Java-index,Desktop,Core GUI APIs...
# 2
You're missing:labellay.fill = GridBagConstraints.BOTH;Mind that if label will be only thing in the panel it will fill whole panel.
hellbindera at 2007-7-14 23:21:52 > top of Java-index,Desktop,Core GUI APIs...
# 3
i tried this already, im not sure really whats going on , i think i will just have to keep trying different things. Thanks both of you.
regularjohna at 2007-7-14 23:21:52 > top of Java-index,Desktop,Core GUI APIs...
# 4

this will put the label top left, but if you uncomment the 2 lines to add the second label

it will be at top middle i.e. 2 columns, both at top left of the columns

could be a lot easier for you to nest a couple of other layout managers

import java.awt.*;

import javax.swing.*;

class Testing extends JFrame

{

public void buildGUI()

{

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(new Dimension(600,400));

setLocationRelativeTo(null);

getContentPane().setLayout(new GridBagLayout());

GridBagConstraints labellay= new GridBagConstraints();

labellay.weightx = 0.5;

labellay.weighty = 0.5;

labellay.gridx=0;

labellay.gridy=0;

labellay.anchor=GridBagConstraints.NORTHWEST;

getContentPane().add(new JLabel("Hello"),labellay);

//labellay.gridx=1;

//getContentPane().add(new JLabel("World"),labellay);

setVisible(true);

}

public static void main(String[] args) {new Testing().buildGUI();}

}

Michael_Dunna at 2007-7-14 23:21:52 > top of Java-index,Desktop,Core GUI APIs...
# 5
ok thanks, that works perfectly!
regularjohna at 2007-7-14 23:21:52 > top of Java-index,Desktop,Core GUI APIs...