Java GridBag Layout Manager help

Ok, wtf. All I want to do is create a grid and add components to it. I have the (apparently mistaken) impression that I simply reference the grid position and add the component, then reference the next grid position, then add the next component.

Apparently not the case, however, as the following snippet of code implies.


import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
//
//
public class PaymentApp {
	//Creates Window
	static JFrame appWindow = new JFrame("Payment Application Window");
	// Window Created
	
	
	public static void main(String[] args) {
		Toolkit theKit = appWindow.getToolkit();
		Dimension windowSize = theKit.getScreenSize();
		
		// The following sets size and position as in
		// (position, position, size, size)
		appWindow.setBounds(windowSize.width/4, windowSize.height/4,
					 windowSize.width/2, windowSize.height/2);
		appWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		// Create the gridbag
		GridBagLayout theGrid = new GridBagLayout();
		GridBagConstraints gridConstraints = new GridBagConstraints();
		appWindow.getContentPane().setLayout(theGrid);
		
		// Set Constraints — makes height and width
		gridConstraints.weightx = 100.0;
		gridConstraints.weighty = 100.0;
		gridConstraints.fill = gridConstraints.HORIZONTAL;
				
		// Add first label at 0, 1
		gridConstraints.gridx = 0;
		gridConstraints.gridy = 1;
		JLabel rateLabel = new JLabel("Daily Periodic Rate (%)");
		appWindow.getContentPane().add(rateLabel);
		
		// Add second label at 0, 2
		gridConstraints.gridx = 0;
		gridConstraints.gridy = 2;
		JLabel balanceLabel = new JLabel("Start Balance");
		appWindow.getContentPane().add(balanceLabel);
		
		// Show window
		appWindow.setVisible(true);
		}
	}

I am hoping to have a grid set such that there are two x columns and about 10 y rows.

No matter how I fuck with the “gridConstraints.x” or “.y” they always end up in the same row. What is going on?

[sup]is this really the right forum for a question like this? I know I see questions in MPSIMS sometimes…[/sup]

Try telling it to use the grid bag constraints you’ve set up:


appWindow.getContentPane().add(rateLabel, gridConstraints);

Works perfectly, thank you.