JTree 中的 JPopupMenu 问题

发布于 2024-09-12 05:10:53 字数 230 浏览 8 评论 0原文

我有这个问题。在自定义 JTree 中,我实现了 JPopupMenu 以根据使用 MouseListener 选择的节点显示不同的 JMenuItem。 单击鼠标右键时将显示 JPopupMenu。问题是,如果我不从 PopupMenu 中选择一个项目,而是选择树中的另一个节点(无论是使用右按钮还是左按钮),则树 MouseListener 永远不会捕获此事件 有人能指出我正确的方向来解决这个问题吗?如果有可用的示例,我将不胜感激。 谢谢。

I have this issue. In a custom JTree I implemented a JPopupMenu to display different JMenuItem according to the node selected using a MouseListener.
The JPopupMenu is shown when the mouse right button is clicked. The problem is that if I don’t choose an Item from the PopupMenu but instead I select another node in the tree, either with right or left buttons, this event is never caught by the tree MouseListener
Could anyone point me in the right direction to solve this? In case an example is available I’ll appreciate it.
Thanks.

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

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

发布评论

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

评论(1

七月上 2024-09-19 05:10:53

我建议也许使用 TreeSelectionListener 来确定所选节点中的更改,而不是使用 MouseListener 并在此时重新填充 JPopupMenu,但这是您的选择。

尝试模拟您的示例,我想知道您在鼠标侦听器中重写了哪些方法?在这个简单的示例中,无论弹出菜单是否显示,侦听器似乎都会获取事件。

编辑 - 请参阅下面的评论,但右键单击不选择节点是默认行为。如果可能的话,本示例将选择距离右键单击位置最近的节点。

public class SampleTree extends JFrame {
    private JPopupMenu menu = new JPopupMenu("Popup");

    public SampleTree() throws HeadlessException {
        super("Tree");
        final JTree tree = new JTree();

        tree.addMouseListener(new MouseAdapter() {
           public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    TreePath tp = tree.getClosestPathForLocation(e.getX(),e.getY());
                    if (tp != null) {
                        System.out.println(tp);
                        tree.setSelectionPath(tp);
                    }
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        String letters = "ABCDEF";

        for (final char letter : letters.toCharArray()) {
            JMenuItem item = new JMenuItem(String.valueOf(letter));
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(SampleTree.this, "You chose the letter: " + letter);
                }
            });
            menu.add(item);
        }

        add(new JScrollPane(tree));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SampleTree st = new SampleTree();
                st.setSize(200, 200);
                st.setLocationRelativeTo(null);
                st.setVisible(true);
            }
        });
    }
}

I would suggest maybe using a TreeSelectionListener for determining changes in selected node as opposed to the MouseListener and repopulating the JPopupMenu at that point, but that's your choice.

Trying to emulate your example, I was wondering, which methods did you override in your mouse listener? In this simple example, the listener seems to get the events regardless of if the popup menu is showing or not.

EDIT - see my comment below, but the right click not selecting a node is default behavior. This example will select the closest node to where the right click was made if possible.

public class SampleTree extends JFrame {
    private JPopupMenu menu = new JPopupMenu("Popup");

    public SampleTree() throws HeadlessException {
        super("Tree");
        final JTree tree = new JTree();

        tree.addMouseListener(new MouseAdapter() {
           public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    TreePath tp = tree.getClosestPathForLocation(e.getX(),e.getY());
                    if (tp != null) {
                        System.out.println(tp);
                        tree.setSelectionPath(tp);
                    }
                    menu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        String letters = "ABCDEF";

        for (final char letter : letters.toCharArray()) {
            JMenuItem item = new JMenuItem(String.valueOf(letter));
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(SampleTree.this, "You chose the letter: " + letter);
                }
            });
            menu.add(item);
        }

        add(new JScrollPane(tree));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                SampleTree st = new SampleTree();
                st.setSize(200, 200);
                st.setLocationRelativeTo(null);
                st.setVisible(true);
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文