Swing/RowSelection のバックアップソース(No.27)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/RowSelection へ行く。
- 1 (2005-04-28 (木) 04:33:09)
- 2 (2005-06-16 (木) 03:17:46)
- 3 (2005-10-16 (日) 16:41:08)
- 4 (2006-01-23 (月) 19:04:27)
- 5 (2006-02-27 (月) 16:20:57)
- 6 (2006-06-22 (木) 17:19:10)
- 7 (2006-09-12 (火) 16:15:20)
- 8 (2006-09-12 (火) 17:24:39)
- 9 (2007-08-14 (火) 12:10:18)
- 10 (2007-10-30 (火) 18:02:55)
- 11 (2007-10-30 (火) 21:06:04)
- 12 (2009-10-16 (金) 19:37:37)
- 13 (2009-10-17 (土) 00:07:38)
- 14 (2010-04-16 (金) 19:27:05)
- 15 (2011-02-23 (水) 19:08:57)
- 16 (2011-08-02 (火) 15:31:26)
- 17 (2012-06-17 (日) 15:27:13)
- 18 (2013-04-09 (火) 22:04:30)
- 19 (2013-07-26 (金) 01:56:13)
- 20 (2013-08-20 (火) 14:25:31)
- 21 (2015-02-05 (木) 14:38:14)
- 22 (2016-09-02 (金) 12:12:23)
- 23 (2017-10-15 (日) 20:15:30)
- 24 (2019-04-25 (木) 17:35:55)
- 25 (2019-05-22 (水) 19:35:38)
- 26 (2021-02-07 (日) 14:54:44)
- 27 (2024-03-30 (土) 23:38:46)
--- category: swing folder: RowSelection title: JTableで行を選択 tags: [JTable, ListSelectionListener] author: aterai pubdate: 2004-05-24T05:21:46+09:00 description: JTableで、行を選択した場合の動作をテストします。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTSWRoWNRI/AAAAAAAAAio/X-jqAVKs3Bw/s800/RowSelection.png --- * 概要 [#summary] `JTable`で、行を選択した場合の動作をテストします。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTSWRoWNRI/AAAAAAAAAio/X-jqAVKs3Bw/s800/RowSelection.png) * サンプルコード [#sourcecode] #code(link){{ table = new JTable(model); table.setAutoCreateRowSorter(true); table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) { return; } int sc = table.getSelectedRowCount(); changeInfoPanel((sc == 1) ? getInfo() : " "); } }); // ... private String getInfo() { int index = table.convertRowIndexToModel(table.getSelectedRow()); String str = (String) model.getValueAt(index, 0); Integer idx = (Integer) model.getValueAt(index, 1); Boolean flg = (Boolean) model.getValueAt(index, 2); return String.format("%s, %d, %s", str, idx, flg); } }} * 解説 [#explanation] 上記のサンプルでは、マウス、カーソルキー、KBD{Tab}キーでの選択状態の変更に対応するため、`JTable`に`MouseListener`リスナーを設定するのではなく`JTable#getSelectionModel`メソッドで`ListSelectionModel`を参照し、このモデルに`ListSelectionListener`リスナーを追加しています。 - `ListSelectionEvent#getValueIsAdjusting()`メソッドでイベントが重複処理を起こさないよう設定 * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#getSelectionModel-- JTable#getSelectionModel() (Java Platform SE 8)] * コメント [#comment] #comment - ありがとう。助かります。 -- &user(ごん); &new{2009-10-16 (金) 19:37:37}; -- どうもです。 -- &user(aterai); &new{2009-10-17 (土) 00:09:01}; - どうもです。 -- &user(ごん?); &new{2011-08-02 (火) 15:31:26}; #comment