pic1.jpg

I am having trouble trying to position labels etc using the gridbaglayout. From the pic above the textfield and the label are too far apart and i'm unable to position the text field closer to the label vision and the same for the checkbox and the cop label.

I've tried all the techniques through research and i'm still unable to position object where i want to

I would be grateful if I could get some assistance please.

Cheers.

private GridBagConstraints con = new GridBagConstraints();
 
public void setUpActor(){
mainFrame = new JFrame("Config Actor");
copLabel1 = new JLabel("Cop");
visionLabel = new JLabel("Vision");
title = new JLabel("Set up Actors");
title.setFont(new Font("Arial", Font.PLAIN, 20));
copChkBox1 = new JCheckBox();
topPanel = new JPanel();
 
 
topPanel.setLayout(new GridBagLayout());
		con.gridx = 0;
		con.gridy = 0;
		con.gridwidth = 3;
		con.weighty = 1.0;
		topPanel.add(title, con);
 
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.gridx = 0;
		con.gridy = 1;
 
		topPanel.add(visionLabel, con);
 
		con.fill = GridBagConstraints.NONE;
		con.weightx = 0.5;
		con.gridx = 1;
		con.gridy = 1;
		topPanel.add(vTxtField, con);
 
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.gridx = 0;
		con.gridy = 2;
		topPanel.add(copLabel1, con);
 
		con.fill = GridBagConstraints.NONE;
		con.weightx = 0.5;
		con.gridx = 1;
		con.gridy = 2;
		topPanel.add(copChkBox1, con);
 
 
                mainFrame.getContentPane().add(BorderLayout.NORTH, topPanel);
		mainFrame.pack();
		mainFrame.setSize(400, 300);
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}