Swing/TreeNodePopupMenu のバックアップ(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - 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)
- title: JTreeのノード上でJPopupMenuを表示 tags: [JTree, JPopupMenu, TreePath] author: aterai pubdate: 2009-06-01T15:04:19+09:00 description: JTreeのノード上でクリックした場合のみ、JPopupMenuを表示します。
概要
JTree
のノード上でクリックした場合のみ、JPopupMenu
を表示します。
Screenshot

Advertisement
サンプルコード
static class TreePopupMenu extends JPopupMenu {
private TreePath[] tsp;
public TreePopupMenu() {
super();
add(new AbstractAction("path") {
@Override public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
null, tsp, "path", JOptionPane.INFORMATION_MESSAGE);
}
});
add(new JMenuItem("dummy"));
}
@Override 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);
}
}
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは以下の場合、JPopupMenu
を表示しています。
JTree
のノードが選択されている- 選択されたノード上にカーソルがある