Swing/TreeNodePopupMenu のバックアップソース(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TreeNodePopupMenu へ行く。
TITLE:JTreeのノード上でJPopupMenuを表示 #navi(../) RIGHT:Posted by &author(aterai); at 2009-06-01 *JTreeのノード上でJPopupMenuを表示 [#u7d9276c] JTreeのノード上でクリックした場合のみ、JPopupMenuを表示します。 -&jnlp; -&jar; -&zip; //#screenshot #ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTWLWQUjBI/AAAAAAAAAo0/3F3RUbU5sx8/s800/TreeNodePopupMenu.png) **サンプルコード [#i85dc25c] #code(link){{ static class TreePopupMenu extends JPopupMenu { private TreePath[] tsp; public TreePopupMenu() { super(); add(new AbstractAction("path") { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, tsp, "path", JOptionPane.INFORMATION_MESSAGE); } }); add(new JMenuItem("dummy")); } public void show(Component c, int x, int y) { JTree tree = (JTree)c; tsp = tree.getSelectionPaths(); if(tsp!=null) { TreePath path = tree.getPathForLocation(x, y); if(path!=null && Arrays.asList(tsp).contains(path)) { super.show(c, x, y); } } } } }} **解説 [#kb3d3d05] 上記のサンプルでは以下の場合、JPopupMenuを表示しています。 -JTreeのノードが選択されている -選択されたノード上にカーソルがある //**参考リンク **コメント [#jef30594] #comment