Google Tag Manager

Showing posts with label SpringLayout. Show all posts
Showing posts with label SpringLayout. Show all posts

2018/04/26

aterai

Move large numbers of items to another JList as fast as possible

Code

private static <E> void move1(JList<E> from, JList<E> to) {
  ListSelectionModel sm = from.getSelectionModel();
  int[] selectedIndices = from.getSelectedIndices();

  DefaultListModel<E> fromModel = (DefaultListModel<E>) from.getModel();
  DefaultListModel<E> toModel = (DefaultListModel<E>) to.getModel();
  List<E> unselectedValues = new ArrayList<>();
  for (int i = 0; i < fromModel.getSize(); i++) {
    if (!sm.isSelectedIndex(i)) {
      unselectedValues.add(fromModel.getElementAt(i));
    }
  }
  if (selectedIndices.length > 0) {
    for (int i: selectedIndices) {
      toModel.addElement(fromModel.get(i));
    }
    fromModel.clear();
    unselectedValues.forEach(fromModel::addElement);
  }
}

References

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