Google Tag Manager

2009/04/06

Animating JTable Rows

Code

public void createActionPerformed(JTable table, DefaultTableModel model) {
  model.addRow(new Object[] {"New name", model.getRowCount(), false});
  int index = table.convertRowIndexToView(model.getRowCount() - 1);
  AtomicInteger height = new AtomicInteger(START_HEIGHT);
  new Timer(DELAY, e -> {
    int h = height.getAndIncrement();
    if (h < END_HEIGHT) {
      table.setRowHeight(index, h);
    } else {
      ((Timer) e.getSource()).stop();
    }
  }).start();
}

public void deleteActionPerformed(JTable table, DefaultTableModel model) {
  int[] selection = table.getSelectedRows();
  if (selection.length == 0) {
    return;
  }
  AtomicInteger height = new AtomicInteger(END_HEIGHT);
  new Timer(DELAY, e -> {
    int h = height.getAndDecrement();
    if (h > START_HEIGHT) {
      for (int i = selection.length - 1; i >= 0; i--) {
        table.setRowHeight(selection[i], h);
      }
    } else {
      ((Timer) e.getSource()).stop();
      for (int i = selection.length - 1; i >= 0; i--) {
        model.removeRow(table.convertRowIndexToModel(selection[i]));
      }
    }
  }).start();
}

References

No comments:

Post a Comment