I'm new to Java Swing and I have been struggling to start the GridBagLayout from top left corner so that c.gridx=0 c.gridy=0 will put my object on the top left corner.
I'd appreciate if you could help me by telling what I need to do after this point:
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
You need to use your
anchor property. This should do it for you:
frame.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.NORTHWEST; frame.add(panel, gbc);
I'm not guaranteeing that you won't have to set other properties of the constraints object to get the layout you desire. In particular, you may need to set
widthy to be
1 so that the panel takes up all of the available space given to it.