Swing/TreeClearSelection のバックアップソース(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TreeClearSelection へ行く。
- 1 (2010-12-06 (月) 14:44:47)
- 2 (2010-12-06 (月) 18:11:37)
- 3 (2010-12-17 (金) 21:19:12)
- 4 (2010-12-18 (土) 02:55:07)
- 5 (2012-12-25 (火) 11:25:26)
- 6 (2013-10-11 (金) 12:53:36)
- 7 (2015-02-05 (木) 14:41:21)
- 8 (2016-09-10 (土) 23:43:03)
- 9 (2017-10-25 (水) 15:44:05)
- 10 (2019-04-01 (月) 18:17:21)
- 11 (2019-10-28 (月) 20:49:57)
- 12 (2021-05-22 (土) 04:40:38)
--- title: JTreeの選択状態を解除する tags: [JTree, MouseListener] author: aterai pubdate: 2010-12-06T14:44:47+09:00 description: JTreeでノード以外の領域をマウスでクリックした場合、選択状態を解除します。 --- * 概要 [#kdaff136] `JTree`でノード以外の領域をマウスでクリックした場合、選択状態を解除します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWDzni-uI/AAAAAAAAAoo/r6UW4JENwgI/s800/TreeClearSelection.png) * サンプルコード [#v9dc8a42] #code(link){{ tree.addMouseListener(new MouseAdapter() { @Overridepublic 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 #comment