概要
JTable
の行追加や削除をスライドアニメーションで強調します。
サンプルコード
private void testCreateActionPerformed(ActionEvent e) {
model.addTest(new Test("New name", ""));
(new Timer(DELAY, new ActionListener() {
int i = table.convertRowIndexToView(model.getRowCount() - 1);
int h = START_HEIGHT;
@Override public void actionPerformed(ActionEvent e) {
if (h < END_HEIGHT) {
table.setRowHeight(i, h++);
} else {
((Timer) e.getSource()).stop();
}
}
})).start();
}
private void deleteActionPerformed(ActionEvent evt) {
final int[] selection = table.getSelectedRows();
if (selection.length <= 0) {
return;
}
(new Timer(DELAY, new ActionListener() {
int h = END_HEIGHT;
@Override public void actionPerformed(ActionEvent e) {
h--;
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();
}
view all解説
上記のサンプルでは、javax.swing.Timer
を使用して徐々に行の高さを拡大、または縮小することで、追加と削除のアニメーションを行っています。
- 行の追加アニメーション
- 高さ
0
の行を追加したあとJTable#setRowHeight(int, int)
メソッドを使用してその高さをデフォルトの高さになるまで拡大
- 高さ
- 行の削除アニメーション
- 選択された行の高さを
JTable#setRowHeight(int, int)
メソッドを使用して縮小 - 高さが一定以下になったらその行を削除
- 選択された行の高さを