JTree 中的 JPopupMenu 问题
我有这个问题。在自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议也许使用 TreeSelectionListener 来确定所选节点中的更改,而不是使用 MouseListener 并在此时重新填充 JPopupMenu,但这是您的选择。
尝试模拟您的示例,我想知道您在鼠标侦听器中重写了哪些方法?在这个简单的示例中,无论弹出菜单是否显示,侦听器似乎都会获取事件。
编辑 - 请参阅下面的评论,但右键单击不选择节点是默认行为。如果可能的话,本示例将选择距离右键单击位置最近的节点。
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.