Swing/TreeNodePopupMenu のバックアップの現在との差分(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/TreeNodePopupMenu へ行く。
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: TreeNodePopupMenu title: JTreeのノード上でJPopupMenuを表示 tags: [JTree, JPopupMenu, TreePath] author: aterai pubdate: 2009-06-01T15:04:19+09:00 description: JTreeのノード上でクリックした場合のみ、JPopupMenuを表示します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWLWQUjBI/AAAAAAAAAo0/3F3RUbU5sx8/s800/TreeNodePopupMenu.png --- * 概要 [#u7d9276c] * 概要 [#summary] `JTree`のノード上でクリックした場合のみ、`JPopupMenu`を表示します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWLWQUjBI/AAAAAAAAAo0/3F3RUbU5sx8/s800/TreeNodePopupMenu.png) * サンプルコード [#i85dc25c] * サンプルコード [#sourcecode] #code(link){{ static class TreePopupMenu extends JPopupMenu { private TreePath[] tsp; public TreePopupMenu() { class TreePopupMenu extends JPopupMenu { protected TreePopupMenu() { super(); add(new AbstractAction("path") { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog( null, tsp, "path", JOptionPane.INFORMATION_MESSAGE); } add("path").addActionListener(e -> { JTree tree = (JTree) getInvoker(); JOptionPane.showMessageDialog( tree, tree.getSelectionPaths(), "path", JOptionPane.INFORMATION_MESSAGE); }); add(new JMenuItem("dummy")); add("JMenuItem"); } @Override public void show(Component c, int x, int y) { JTree tree = (JTree) c; tsp = tree.getSelectionPaths(); if (tsp != null) { if (c instanceof JTree) { JTree tree = (JTree) c; TreePath path = tree.getPathForLocation(x, y); if (path != null && Arrays.asList(tsp).contains(path)) { if (tree.getSelectionCount() > 0 && Arrays.asList(tree.getSelectionPaths()).contains(path)) { super.show(c, x, y); } } } } }} * 解説 [#kb3d3d05] 上記のサンプルでは以下の場合、`JPopupMenu`を表示しています。 * 解説 [#explanation] 上記のサンプルでは、`JTree`のノードが選択されている、かつ選択されたノード上にカーソルがある場合のみ`JPopupMenu`を表示可能に設定しています。 - `JTree`のノードが選択されている - 選択されたノード上にカーソルがある * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTree.html#getPathForLocation-int-int- JTree#getPathForLocation(...) (Java Platform SE 8)] //* 参考リンク * コメント [#jef30594] * コメント [#comment] #comment #comment