0

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.

enter image description here

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.

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Edward Falk
  • 9,991
  • 11
  • 77
  • 112

2 Answers2

0

Well, no answer after two days = find another way. I wound up using GridBagLayout, which I should have done in the first place. I'll leave the question open just in case someone answers it some day.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
0

So this is a very old question, but I had a similar issue with right justify using SpringLayout.

What worked for me is to align things from the left, and to size the components to fit 'up to' the right edge of your panel. (which may not be the best solution)

For example, if I had four boxes I wanted to align:

| box1 | box2 |

| box3 | box4 |

box1 would be:

layout.putConstraint(SpringLayout.NORTH, box1, 2, SpringLayout.NORTH, buttonPanel);
layout.putConstraint(SpringLayout.WEST, box1, 2, SpringLayout.WEST, buttonPanel);

box2 would be:

layout.putConstraint(SpringLayout.NORTH, box2, 2, SpringLayout.NORTH, buttonPanel);
layout.putConstraint(SpringLayout.WEST, box1, 2, SpringLayout.EAST, box1);

box3 would be:

layout.putConstraint(SpringLayout.NORTH, box3, 2, SpringLayout.SOUTH, box1);
layout.putConstraint(SpringLayout.WEST, box3, 2, SpringLayout.WEST, buttonPanel);

box4 would be:

layout.putConstraint(SpringLayout.NORTH, box4, 2, SpringLayout.SOUTH, box2);
layout.putConstraint(SpringLayout.WEST, box1, 2, SpringLayout.EAST, box3);
4ndrew
  • 11
  • 2
  • That looks like it would pack all the boxes in the north-west corner of the container. What i want is for box4 to sit at the right edge of the container, even if the container is bigger than it needs to be to hold the boxes. – Edward Falk Aug 03 '17 at 23:06