• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: SlideTableRows
title: JTableで行の追加、削除アニメーション
tags: [JTable, Animation]
author: aterai
pubdate: 2009-04-06T14:03:11+09:00
description: JTableの行追加や削除をスライドアニメーションで強調します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTP0i2yxI/AAAAAAAAAkE/DQKpmn3BIQo/s800/SlideTableRows.png
hreflang:
    href: http://java-swing-tips.blogspot.com/2009/04/animating-jtable-rows.html
    href: https://java-swing-tips.blogspot.com/2009/04/animating-jtable-rows.html
    lang: en
---
* 概要 [#summary]
`JTable`の行追加や削除をスライドアニメーションで強調します。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTP0i2yxI/AAAAAAAAAkE/DQKpmn3BIQo/s800/SlideTableRows.png)

* サンプルコード [#sourcecode]
#code(link){{
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();
}
}}

* 解説 [#explanation]
上記のサンプルでは、`javax.swing.Timer`を使用して、徐々に行の高さを変更することで、アニメーションを行っています。

- 行の追加
-- 高さ`0`の行を追加したあと、`JTable#setRowHeight(int, int)`メソッドを使用してその高さを変更

- 行の削除
-- 選択された行の高さを、`JTable#setRowHeight(int, int)`メソッドを使用して変更
-- 高さが一定以下になったら、その行を削除

* 参考リンク [#reference]
- [[JTableの行を追加、削除>Swing/AddRow]]
- [[JTableの行の高さを変更する>Swing/FishEyeTable]]

* コメント [#comment]
#comment
#comment