TITLE:JTreeの選択状態を解除する
#navi(../)
#tags(JTree, MouseListener)
RIGHT:Posted by &author(aterai); at 2010-12-06
* JTreeの選択状態を解除する [#kdaff136]
``JTree``でノード以外の領域をマウスでクリックした場合、選択状態を解除します。

- &jnlp;
- &jar;
- &zip;

#ref(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWDzni-uI/AAAAAAAAAoo/r6UW4JENwgI/s800/TreeClearSelection.png)

** サンプルコード [#v9dc8a42]
#code(link){{
tree.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent e) {
    JTree tree = (JTree)e.getSource();
    if(tree.getRowForLocation(e.getX(), e.getY())<0) {
      tree.clearSelection();
    }
  }
});
}}

** 解説 [#x8c210f4]
上記のサンプルでは、``JTree#getRowForLocation(...)``メソッドを使用して、``JTree``のノード以外のポイントがクリックされたかどうかを判断しています。ノードの選択解除自体は、``JTree#clearSelection()``が使用できます。

** 参考リンク [#u6021f39]
- [http://docs.oracle.com/javase/jp/6/api/javax/swing/JTree.html JTree (Java Platform SE 6)]のサンプルコード

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