JList MouseMoved 和 MousePressed

发布于 2024-12-19 07:54:01 字数 956 浏览 1 评论 0 原文

我扩展了 JList 以提供两个独立的功能:项目的 toolTipText 和右键单击选项。两者都是单独工作的,但是当我尝试一起使用它们时, MouseMoved 事件无法被识别?以下是我的新侦听器方法的核心内容。我应该如何谈判这些不同的事件?

public class JListTT extends javax.swing.JList {
    public JListTT() {
        super();
       addMouseListener(new ttListener());
...
   class ttListener extends MouseAdapter {
        public void mouseMoved(MouseEvent e) {
             String nodeID = bldItemNodeID();
             theList.setToolTipText(nodeID);
            }
        public void mousePressed(MouseEvent ev)  {check(ev); }
        public void mouseReleased(MouseEvent ev) {check(ev); }
        public void mouseClicked(MouseEvent ev)  {check(ev); }
        public void check(MouseEvent ev) {
            if (ev.isPopupTrigger()) { 
                theList.setSelectedIndex(theList.locationToIndex(ev.getPoint())); 
                menu.show(theList, ev.getX(), ev.getY()); 
            }
        }
    }

I've extended a JList to provide two separate functionalities, toolTipText for items, and right-click options. Both work separately, but when I try to use them together, the MouseMoved events aren't being recognized? Below are the guts of my new listener methods. How should I be negotiating these various events?

public class JListTT extends javax.swing.JList {
    public JListTT() {
        super();
       addMouseListener(new ttListener());
...
   class ttListener extends MouseAdapter {
        public void mouseMoved(MouseEvent e) {
             String nodeID = bldItemNodeID();
             theList.setToolTipText(nodeID);
            }
        public void mousePressed(MouseEvent ev)  {check(ev); }
        public void mouseReleased(MouseEvent ev) {check(ev); }
        public void mouseClicked(MouseEvent ev)  {check(ev); }
        public void check(MouseEvent ev) {
            if (ev.isPopupTrigger()) { 
                theList.setSelectedIndex(theList.locationToIndex(ev.getPoint())); 
                menu.show(theList, ev.getX(), ev.getY()); 
            }
        }
    }

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

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

发布评论

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

评论(4

眼角的笑意。 2024-12-26 07:54:01

您将 ttListener 对象添加为 MouseListener,但我没有看到您将 ttListener 对象添加为 MouseMotionListener。例如:

ttListener myMouseadapter = new ttListener();
addMouseListener(myMouseadapter);
addMouseMotionListener(myMouseadapter);

You add the ttListener object as a MouseListener, but I don't see you adding the ttListener object as a MouseMotionListener. For example:

ttListener myMouseadapter = new ttListener();
addMouseListener(myMouseadapter);
addMouseMotionListener(myMouseadapter);
凶凌 2024-12-26 07:54:01

我自己没有对此进行测试,但查看 JList 的 javadoc 工具提示功能是开箱即用的。 JList#getTooltipText 明确指出

重写 JComponent 的 getToolTipText 方法以允许
如果设置了文本,则使用渲染器的提示。

因此,如果您的 ListCellRenderergetListCellRendererComponent 方法中返回一个 Component,该方法具有 工具提示 它将由 JList 显示,无需倾听者的需要。

I did not test this myself, but looking at the javadoc of JList the tooltip functionality is available out of the box. The javadoc of JList#getTooltipText clearly states

Overrides JComponent's getToolTipText method in order to allow the
renderer's tips to be used if it has text set.

So if your ListCellRenderer returns a Component in the getListCellRendererComponent method which has a tooltip it will be displayed by the JList without the need of a listener.

呢古 2024-12-26 07:54:01

不一定需要像自定义鼠标/motionListener这样的低级方法:

  • 对于每个单元格的工具提示,请参阅@Robin
  • 关于上下文菜单的答案,JComonent有一个属性componentPopupMenu:使用它将处理打开键盘快捷键上的菜单自动

“不一定”,因为您似乎依赖于右键单击时选择的单元格。如果是这样,您仍然需要一个 MouseListener 来触发选择(经过十年的争论,Swing 不需要 - 这在当前的本机应用程序中似乎很不寻常;-)

there's not necessarily a need for a low-level approach as a custom mouse-/motionListener:

  • as to a per-cell tooltip, see @Robin's answer
  • as to a context menu, JComonent has a property componentPopupMenu: using that will cope with opening the menu on keyboard short-cut automatically

"not necessarily" because you seem to rely on the cell being selected on right click. If so, you would still need a MouseListener to trigger the selection (after decade long debates, Swing doesn't - which seems to be unusual in current native apps ;-)

枯寂 2024-12-26 07:54:01

您可以通过使用 mouseDragged 来实现

YourClass extends JPanel implements MouseListener{
    ......

    @Override
    public void mouseDragged(MouseEvent e) {
        //code go here

    }
}

You can achieve it by using mouseDragged

YourClass extends JPanel implements MouseListener{
    ......

    @Override
    public void mouseDragged(MouseEvent e) {
        //code go here

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文