JLabel ToolTip interferes with MouseListener

删除回忆录丶 提交于 2019-12-05 08:42:59

It is working as intended. Let me explain why.

When you are adding a tooltip to any component (labels in your case) they automatically recieve a new mouse listeners from ToolTipManager. Here is the register method from ToolTipManager class:

public void registerComponent(JComponent component) {
    component.removeMouseListener(this);
    component.addMouseListener(this);
    component.removeMouseMotionListener(moveBeforeEnterListener);
    component.addMouseMotionListener(moveBeforeEnterListener);
    component.removeKeyListener(accessibilityKeyListener);
    component.addKeyListener(accessibilityKeyListener);
}

When any component has atleast one mouse listener set on it - it will block any mouse enter/exit/click/press/release events (mouse dragged/moved in case there is mouse motion listener set) from going down in the components hierarchy.

In your case - labels blocking mouse events and mouse motion events from going down to layered pane due to ToolTipManager listeners installed when tooltip is set.

This could be avoided if you make a workaround listener with that will pass events down. For example you can add that listener to every component with a tooltip that should pass mouse events down.

Here is a small example of how that could be done:

label.addMouseListener ( new MouseAdapter ()
{
    public void mousePressed ( MouseEvent e )
    {
        lpane.dispatchEvent ( SwingUtilities.convertMouseEvent ( e.getComponent (), e, lpane ) );
    }
} );

In that case event will be passed to layered pane though. Anyway you can dispatch this even anywhere you want (i guess it would be spane in your case).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!