I'm trying to create a layout that has a few text items at the left, and a button at the right. I've got the text items exactly the way I want them, but I can't get the button aligned at the right.
I'm creating the button as follows:
SpringLayout layout = new SpringLayout();
JPanel p2 = new JPanel(layout);
// set panel size very large so it fills its own parent
p2.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
p2.setBackground(new Color(0xffd0d0));
p2.setBorder(BorderFactory.createLineBorder(new Color(0)));
// Add some text items; omitted for clarity
...
// Add a button in the lower-right corner
JButton btn = new JButton(refreshAction);
p2.add(btn);
layout.putConstraint(SpringLayout.EAST, btn,
Spring.constant(0),
SpringLayout.EAST, p2);
layout.putConstraint(SpringLayout.SOUTH, btn,
Spring.constant(0),
SpringLayout.SOUTH, p2);
I thought this would align the East and South edges of the button with the East and South edges of the container, but it's not happening. It looks like the button edges are being aligned with the container's preferred size rather than its actual size.
One more data point: When I set the values for the labels, the Button jumps to the right, aligning itself with the end of the just-added text. Clearly the preferred size of the container has increased, even if the actual size hasn't changed, and the button's position changed in response to that.