Java中Swing中右键单击节点添加JPopup菜单

发布于 2024-11-16 17:22:55 字数 246 浏览 4 评论 0原文

在 GUI 中,我在 JPanel 的左侧显示一个 JTree。现在,对于每个Node(leaf),在鼠标右键单击时,我想显示JPopup菜单,要求在右侧中显示有关Node的统计信息>JPanel

由于我是摇摆新手,任何人都可以帮助编写代码吗? 提前致谢。

问候, 图沙尔·多迪亚。

In GUI,I am displaying one JTree at the left hand side of JPanel. Now for each Node(leaf), on Mouse right click I want to display JPopup menu asking for displaying the statistics about Node in right JPanel.

As i am new to swing,Could any one help with code.
Thanks in Advance.

Regards,
Tushar Dodia.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青瓷清茶倾城歌 2024-11-23 17:22:55

使用 JTree 的方法

public TreePath getPathForLocation(int x, int y)

然后 TreePath

public Object getLastPathComponent()

从用户右键单击的点返回所需的节点。

Use JTree's method

public TreePath getPathForLocation(int x, int y)

Then TreePath

public Object getLastPathComponent()

That returns you desired node from point where user right clicked.

心如荒岛 2024-11-23 17:22:55

似乎引起了一些混乱(让我自己感到困惑;-) - 所以这里有一个代码片段,用于对 componentPopup 进行目标位置相关配置

    JPopupMenu popup = new JPopupMenu();
    final Action action = new AbstractAction("empty") {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
        }
    };
    popup.add(action); 
    JTree tree = new JTree() {

        /** 
         * @inherited <p>
         */
        @Override
        public Point getPopupLocation(MouseEvent e) {
            if (e != null) {
               TreePath path = getClosestPathForLocation(e.getX(), e.getY());
               action.putValue(Action.NAME, String.valueOf(path.getLastPathComponent()));
               return e.getPoint();
            }
            action.putValue(Action.NAME, "no mouse"); 
            return null;
        }

    };
    tree.setComponentPopupMenu(popup);

Seem to have caused a bit of confusion (confusing myself ;-) - so here's a code snippet for doing target location related configuration of the componentPopup

    JPopupMenu popup = new JPopupMenu();
    final Action action = new AbstractAction("empty") {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
        }
    };
    popup.add(action); 
    JTree tree = new JTree() {

        /** 
         * @inherited <p>
         */
        @Override
        public Point getPopupLocation(MouseEvent e) {
            if (e != null) {
               TreePath path = getClosestPathForLocation(e.getX(), e.getY());
               action.putValue(Action.NAME, String.valueOf(path.getLastPathComponent()));
               return e.getPoint();
            }
            action.putValue(Action.NAME, "no mouse"); 
            return null;
        }

    };
    tree.setComponentPopupMenu(popup);
十二 2024-11-23 17:22:55

我采用了@kleopatra 解决方案并对其进行了轻微更改。
也许这不是最好的方法,但对我有用。

JTree tree = new JTree() {
    private static final long serialVersionUID = 1L;
    @Override public Point getPopupLocation(MouseEvent e) {
        if (e == null) return new Point(0,0);
        TreePath path = getClosestPathForLocation(e.getX(), e.getY());
        Object selected = path != null ? path.getLastPathComponent() : null;
        setComponentPopupMenu(getMenuForTreeNode(getComponentPopupMenu(), selected));
        setSelectionPath(path);
        return e.getPoint();
    }
};



public JPopupMenu getMenuForTreeNode(JPopupMenu menu, Object treeNode) {
    if (menu == null) menu = new JPopupMenu("Menu:");
    menu.removeAll();
    if (treeNode instanceof MyTreeItem) {
        menu.add(new JMenuItem("This is my tree item: " + treeNode.toString()));
    }
    return menu;
}

I took @kleopatra solution and changed it slightly.
Maybe it isn't the best way but works for me.

JTree tree = new JTree() {
    private static final long serialVersionUID = 1L;
    @Override public Point getPopupLocation(MouseEvent e) {
        if (e == null) return new Point(0,0);
        TreePath path = getClosestPathForLocation(e.getX(), e.getY());
        Object selected = path != null ? path.getLastPathComponent() : null;
        setComponentPopupMenu(getMenuForTreeNode(getComponentPopupMenu(), selected));
        setSelectionPath(path);
        return e.getPoint();
    }
};



public JPopupMenu getMenuForTreeNode(JPopupMenu menu, Object treeNode) {
    if (menu == null) menu = new JPopupMenu("Menu:");
    menu.removeAll();
    if (treeNode instanceof MyTreeItem) {
        menu.add(new JMenuItem("This is my tree item: " + treeNode.toString()));
    }
    return menu;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文