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