如何使工具提示对鼠标事件透明? [英] How to make a Tooltip transparent to mouse events?

查看:127
本文介绍了如何使工具提示对鼠标事件透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javafx.scene.Node 能够使其对鼠标事件透明,因此不会将其选为此类事件的目标:

A javafx.scene.Node has the ability to make it transparent to mouse events, so that it won't be selected as target for such events:


Node.mouseTransparentProperty()

如果为true,则此节点(及其所有子节点)对鼠标事件完全透明。选择鼠标事件的目标时,将mouseTransparent设置为true的节点及其子树将不予考虑。

Node.mouseTransparentProperty()
If true, this node (together with all its children) is completely transparent to mouse events. When choosing target for mouse event, nodes with mouseTransparent set to true and their subtrees won't be taken into account.

不幸的是这个功能尚未实现 javafx.scene.control.Tooltip

有一个打开的功能请求 - 但似乎没有在这个主题上做了很多活动。

Unfortunately this features is not yet implemented for javafx.scene.control.Tooltip.
There is an open feature request for that - but there doesn't seem to be a lot of activity on that topic.

我的问题是:有没有解决方法?如何将鼠标事件的工具提示透明化以将鼠标事件路由到基础控件?

My question is: Is there any workaround for this? How can I make a Tooltip mouse-transparent to route mouse events to the underlying control?

推荐答案

如果有人仍在寻找解。我在javafx-8中找到了一种hacky方式(使用内部API !!)。它可以在javafx-8 +中进行修补,因此从维护性的角度来看,这不是一个好的选择,但至少有一些:

If someone is still looking for a solution. I found a hacky way in javafx-8 (using internal API!!). Its propably patched in javafx-8+, so from a maintainablility standpoint not a good option, but at least something:

    public static boolean correctNativeMouseEvent(MouseEvent event, Scene exclude)
    {
        Scene targetScene = getTargetScreen(event, exclude);
        if(targetScene != null)
        {
            PickResultChooser chooser = new PickResultChooser();

            targetScene.getRoot().impl_pickNode(new PickRay(event.getScreenX() - targetScene.getWindow().getX() - targetScene.getX(),
                    event.getScreenY() - targetScene.getWindow().getY() - targetScene.getY(),
                    1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), chooser);

            PickResult res = chooser.toPickResult();
            if(res != null)
            {
                Point2D pos = res.getIntersectedNode().localToScene(res.getIntersectedPoint().getX(), res.getIntersectedPoint().getY());

                MouseEvent newEvent = new MouseEvent(null, null, event.getEventType(), pos.getX(), pos.getY(),
                        event.getScreenX(), event.getScreenY(),
                        event.getButton(), event.getClickCount(),
                        event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(),
                        event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(),
                        event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(), res);

                targetScene.impl_processMouseEvent(newEvent);
                return true;
            }
        }
        return false;
    }

    static Scene getTargetScreen(MouseEvent event, Scene exclude)
    {
        double x = event.getScreenX();
        double y = event.getScreenY();

        double sx, sy, sw, sh;

        Iterator<Window> itr = Window.impl_getWindows();

        if(itr.hasNext())
        {
            for(Window w = itr.next(); itr.hasNext(); w = itr.next())
            {
                sx = w.getX();
                sy = w.getY();
                sw = w.getWidth();
                sh = w.getHeight();

                if(sx < x && x < sx + sw
                        && sy < y && y < sy + sh
                        && w.getScene() != exclude)
                    return w.getScene();
            }
        }
        return null;
    }

创建工具提示后,您只需添加以下内容:

upon creating a tooltip you just add the following:

Tooltip tp = new Tooltip();
// use filter to catch before anything can be consumed
tp.addEventFilter(MouseEvent.ANY, E -> {
    // now correct the event
    correctNativeMouseEvent(E, tp.getScene());
    // although it is optionally, I would recommend to just consume the event anyways
    E.consume();
};

这篇关于如何使工具提示对鼠标事件透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆