tags:

views:

41

answers:

1

I am making a GUI using SpringLayout using the following code:

private void createAndShowGUI() {
    frame = new JFrame("A GUI");
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.setResizable(false);

    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);

    contentPane.add(this);
    layout.putConstraint(SpringLayout.WEST, this, 300, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, this, 0, SpringLayout.NORTH, contentPane);

    JLabel startLabel = new JLabel("Start Node:");
    contentPane.add(startLabel);
    layout.putConstraint(SpringLayout.WEST, startLabel, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, startLabel, 5, SpringLayout.NORTH, contentPane);

    startNodes = new JComboBox();
    contentPane.add(startNodes);
    layout.putConstraint(SpringLayout.WEST, startNodes, 15, SpringLayout.WEST, contentPane); // THIS LINE
    layout.putConstraint(SpringLayout.EAST, startNodes, -10, SpringLayout.WEST, this); // AND THIS ONE
    layout.putConstraint(SpringLayout.NORTH, startNodes, 5, SpringLayout.SOUTH, startLabel);

    layout.putConstraint(SpringLayout.EAST, contentPane, 0, SpringLayout.EAST, this);
    layout.putConstraint(SpringLayout.SOUTH, contentPane, 0, SpringLayout.SOUTH, this);

    frame.pack();
    frame.setVisible(true);
}`

When I run it (on NetBSD), the two commented lines seem to fight with each other. When I just have the first one, it aligns to the left as expected, but when I add the second one, it aligns to the right, as opposed to stretching to the right as I would expect.

The weird thing is that it runs just fine on Windows 7 Pro 32bit.

Can anyone tell me what I am doing wrong?

+1  A: 

It turns out that in Java 1.5, you always have to specify the EAST/SOUTH constraints before the WEST/NORTH constraints. Otherwise it doesn't position the components properly. This bug is fixed in 1.6, which is why I didn't notice it on my Windows machine.

DanieL