Google Tag Manager

2011/10/28

aterai

fixed width JButtons in bottom right corner of the JPanel

Code

private static JComponent createRightAlignButtonBox2(
    Listlist, int buttonWidth, int buttonHeight, int gap) {
  SpringLayout layout = new SpringLayout();
  JPanel p = new JPanel(layout);
  SpringLayout.Constraints pCons = layout.getConstraints(p);
  pCons.setConstraint(SpringLayout.SOUTH, Spring.constant(buttonHeight+gap+gap));

  Spring x     = layout.getConstraint(SpringLayout.WIDTH, p);
  Spring y     = Spring.constant(gap);
  Spring g     = Spring.minus(Spring.constant(gap));
  Spring width = Spring.constant(buttonWidth);
  for(JButton b: list) {
      SpringLayout.Constraints constraints = layout.getConstraints(b);
      constraints.setConstraint(SpringLayout.EAST, x = Spring.sum(x, g));
      constraints.setY(y);
      constraints.setWidth(width);
      p.add(b);
      x = Spring.sum(x, Spring.minus(width));
  }
  return p;
}

References