Swing/TreeNodePopupMenu のバックアップ(No.13)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/TreeNodePopupMenu へ行く。
- 1 (2012-09-08 (土) 05:18:44)
- 2 (2013-01-09 (水) 20:52:21)
- 3 (2015-01-30 (金) 19:36:23)
- 4 (2015-08-11 (火) 01:50:37)
- 5 (2017-04-05 (水) 18:28:14)
- 6 (2017-10-23 (月) 12:57:51)
- 7 (2019-04-19 (金) 14:07:25)
- 8 (2021-02-02 (火) 08:04:55)
- 9 (2024-02-03 (土) 14:28:34)
- 10 (2024-02-14 (水) 23:37:13)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
- 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:
Summary
JTree
のノード上でクリックした場合のみ、JPopupMenu
を表示します。
Screenshot

Advertisement
Source Code Examples
class TreePopupMenu extends JPopupMenu {
protected TreePopupMenu() {
super();
add("path").addActionListener(e -> {
JTree tree = (JTree) getInvoker();
JOptionPane.showMessageDialog(
tree, tree.getSelectionPaths(), "path",
JOptionPane.INFORMATION_MESSAGE);
});
add("JMenuItem");
}
@Override public void show(Component c, int x, int y) {
if (c instanceof JTree) {
JTree tree = (JTree) c;
TreePath path = tree.getPathForLocation(x, y);
if (tree.getSelectionCount() > 0
&& Arrays.asList(tree.getSelectionPaths()).contains(path)) {
super.show(c, x, y);
}
}
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JTree
のノードが選択されている、かつ選択されたノード上にカーソルがある場合のみJPopupMenu
を表示可能に設定しています。