• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableで行を選択
#navi(../)
*JTableで行を選択 [#jeb5f621]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-05-24~
更新日:&lastmod;
---
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`で、行を選択した場合の動作をテストします。

#contents
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTSWRoWNRI/AAAAAAAAAio/X-jqAVKs3Bw/s800/RowSelection.png)

**概要 [#f064647c]
JTableで、行を選択した場合の動作をテストします。
* サンプルコード [#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);
}
}}

#screenshot
* 解説 [#explanation]
上記のサンプルでは、マウス、カーソルキー、KBD{Tab}キーでの選択状態の変更に対応するため、`JTable`に`MouseListener`リスナーを設定するのではなく`JTable#getSelectionModel`メソッドで`ListSelectionModel`を参照し、このモデルに`ListSelectionListener`リスナーを追加しています。

**サンプルコード [#f054e87d]
 tbl.setRowSelectionAllowed(true);
 tbl.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
 tbl.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
   public void valueChanged(ListSelectionEvent e) {
     if(e.getValueIsAdjusting()) return;
     int sc = tbl.getSelectedRowCount();
     String str;
     if(sc<=0 || sc>1) {
       str = "";
     }else{
       str = getInfo(sorter.modelIndex(tbl.getSelectedRow()));
     }
     changeInfoPanel(str);
   }
 });
- `ListSelectionEvent#getValueIsAdjusting()`メソッドでイベントが重複処理を起こさないよう設定

 private String getInfo(int index) {
   String name = (String)model.getValueAt(index, 1);
   String comment = (String)model.getValueAt(index, 2);
   return name+"( "+comment+" )";
 }
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#getSelectionModel-- JTable#getSelectionModel() (Java Platform SE 8)]

-&jnlp;
-&jar;
-&zip;
* コメント [#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};

**解説 [#b6b30ce9]
マウスやカーソルキー、タブキーでの選択状態の変更に対応する場合は、JTableにMouseListenerリスナーを設定するのではなく、JTable#getSelectionModelメソッドでListSelectionModelを参照し、このモデルにListSelectionListenerリスナーを追加して利用します。

ListSelectionEvent#getValueIsAdjusting()メソッドでイベントが重複処理を起こさないように制御しています。

//**参考リンク
**コメント [#r27a6181]
#comment