Swing/SlideTableRows のバックアップの現在との差分(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SlideTableRows へ行く。
- 1 (2009-04-06 (月) 14:03:11)
- 2 (2011-05-04 (水) 18:58:16)
- 3 (2013-01-10 (木) 17:43:58)
- 4 (2013-12-25 (水) 18:57:48)
- 5 (2014-11-30 (日) 00:44:59)
- 6 (2015-01-25 (日) 18:33:25)
- 7 (2015-08-05 (水) 15:17:18)
- 8 (2017-04-06 (木) 20:38:58)
- 9 (2018-02-24 (土) 19:51:30)
- 10 (2018-03-23 (金) 16:28:34)
- 11 (2020-03-26 (木) 15:08:31)
- 12 (2021-09-30 (木) 09:48:58)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableで行の追加、削除アニメーション #navi(../) #tags(JTable, Animation) RIGHT:Posted by &author(aterai); at 2009-04-06 * JTableで行の追加、削除アニメーション [#l06043c1] --- 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: https://java-swing-tips.blogspot.com/2009/04/animating-jtable-rows.html lang: en --- * 概要 [#summary] `JTable`の行追加や削除をスライドアニメーションで強調します。 - &jnlp; - &jar; - &zip; #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTP0i2yxI/AAAAAAAAAkE/DQKpmn3BIQo/s800/SlideTableRows.png) #ref(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTP0i2yxI/AAAAAAAAAkE/DQKpmn3BIQo/s800/SlideTableRows.png) ** サンプルコード [#qda416f4] * サンプルコード [#sourcecode] #code(link){{ private void testCreateActionPerformed(ActionEvent e) { model.addTest(new Test("New name", "")); (new javax.swing.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{ ((javax.swing.Timer)e.getSource()).stop(); } 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(); }).start(); } private void deleteActionPerformed(ActionEvent evt) { final int[] selection = table.getSelectedRows(); if(selection==null || selection.length<=0) return; (new javax.swing.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{ ((javax.swing.Timer)e.getSource()).stop(); for(int i=selection.length-1;i>=0;i--) model.removeRow(table.convertRowIndexToModel(selection[i])); 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(); }).start(); } }} ** 解説 [#g2277059] 上記のサンプルでは、`javax.swing.Timer`を使用して、徐々に行の高さを変更することで、アニメーションを行っています。 * 解説 [#explanation] 上記のサンプルでは、`javax.swing.Timer`を使用して徐々に行の高さを拡大、または縮小することで、追加と削除のアニメーションを行っています。 - 追加 -- 行を追加したあとで、`JTable#setRowHeight(int, int)`メソッドを使用して追加された行の高さを変更 - 行の追加アニメーション -- 高さ`0`の行を追加したあと`JTable#setRowHeight(int, int)`メソッドを使用してその高さをデフォルトの高さになるまで拡大 - 行の削除アニメーション -- 選択された行の高さを`JTable#setRowHeight(int, int)`メソッドを使用して縮小 -- 高さが一定以下になったらその行を削除 - 削除 -- 選択された行の高さを、`JTable#setRowHeight(int, int)`メソッドを使用して変更 -- 高さが一定以下になったら、選択されていた行を削除 ** 参考リンク [#za2c21bb] * 参考リンク [#reference] - [[JTableの行を追加、削除>Swing/AddRow]] - [[JTableの行の高さを変更する>Swing/FishEyeTable]] ** コメント [#u064865d] * コメント [#comment] #comment #comment