Swing/ClearSortingState のバックアップソース(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ClearSortingState へ行く。
- 1 (2007-08-27 (月) 12:33:13)
- 2 (2007-08-27 (月) 14:45:58)
- 3 (2007-08-27 (月) 18:21:50)
- 4 (2008-04-11 (金) 18:28:37)
- 5 (2008-09-15 (月) 13:50:46)
- 6 (2013-02-01 (金) 11:33:28)
- 7 (2013-07-26 (金) 01:30:04)
- 8 (2015-01-29 (木) 15:38:21)
- 9 (2015-07-23 (木) 17:07:28)
- 10 (2016-01-12 (火) 17:54:13)
- 11 (2017-04-04 (火) 14:17:08)
- 12 (2017-06-23 (金) 12:52:26)
- 13 (2018-06-26 (火) 13:10:57)
- 14 (2020-06-09 (火) 23:50:51)
- 15 (2021-11-25 (木) 05:39:17)
--- title: TableRowSorterのSortKeysをクリアする tags: [JTable, TableRowSorter, MouseListener] author: aterai pubdate: 2007-08-27T12:33:13+09:00 description: JDK 6で導入されたTableRowSorterでの行ソートを、テーブルヘッダのKBD{Shift}+クリックでクリアします。 --- * 概要 [#q8dd9a10] `JDK 6`で導入された`TableRowSorter`での行ソートを、テーブルヘッダのKBD{Shift}+クリックでクリアします。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTJF8YbgvI/AAAAAAAAAT0/NDSO1fqmVNw/s800/ClearSortingState.png) * サンプルコード [#o4d0be64] #code(link){{ table.setAutoCreateRowSorter(true); table.getTableHeader().addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { final RowSorter<? extends TableModel> sorter = table.getRowSorter(); if (sorter == null || sorter.getSortKeys().size() == 0) { return; } JTableHeader h = (JTableHeader) e.getComponent(); TableColumnModel columnModel = h.getColumnModel(); int viewColumn = columnModel.getColumnIndexAtX(e.getX()); if (viewColumn < 0) { return; } int column = columnModel.getColumn(viewColumn).getModelIndex(); if (column != -1 && e.isShiftDown()) { EventQueue.invokeLater(new Runnable() { @Override public void run() { sorter.setSortKeys(null); } }); } } }); }} * 解説 [#sc19e708] ヘッダにマウスリスナーを設定し、KBD{Shift}キーを押しながらのクリックの場合は、`TableRowSorter#setSortKeys`メソッドを使って、ソートキーを空にしています。 上記のサンプルでは、以下のような制限があります。 - ソートキーになっていないヘッダカラムをKBD{Shift}+クリックした場合でも、ソート状態をクリアする - %%行がソートされている場合は、ドラッグ&ドロップで行を入れ替え不可%% `D&D`機能は削除 * 参考リンク [#zd14dfcb] - [http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java TableSorter.java] - [[TableRowSorterでJTableのソート>Swing/TableRowSorter]] - [[TableRowSorterのソートをヘッダクリックで昇順、降順、初期状態に変更>Swing/TriStateSorting]] * コメント [#fc73f66f] #comment #comment