工具提示:如何获取触发提示的鼠标坐标? [英] Tooltip: how to get mouse coordinates that triggered the tip?

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

问题描述

要求:以文本形式显示触发工具提示的mouseEvent的坐标.对于contextMenu,该位置存储在contextMenuEvent中,因此我将侦听contextMenuRequested并根据需要进行更新.

The requirement: show the coordinates of the mouseEvent that triggered the tooltip as its text. For a contextMenu, the location is stored in the contextMenuEvent, so I would listen to contextMenuRequested and update as needed.

找不到与工具提示类似的东西,所以播放了一下(请参见下面的示例):

Couldn't find anything similar for a tooltip, so played a bit (see example below):

  • 在显示/显示时,我可以查询工具提示位置:对于AnchorLocation.CONTENT_TOP_LEFT,其x/y似乎是最后一个鼠标位置,尽管略有增加.不过,可能是偶然的,未指定(因此无法使用),对于其他锚点类型肯定是不可用的

  • at the time of showing/shown, I could query the tooltip location: for AnchorLocation.CONTENT_TOP_LEFT its x/y seems to be about the last mouse location, though slightly increased. Could be accidental, though, is unspecified (and as such unusable) and definitely off for other anchor types

蛮力方法是安装鼠标移动的处理程序并将当前鼠标位置存储到工具提示的属性.不想这样做,因为那是重复的功能,因为ToolTipBehaviour已经跟踪了触发位置,不幸的是像往常一样秘密地位于顶部

the brute force method would be to install a mouse-moved handler and store the current mouse location into the tooltip's properties. Wouldn't like to, because that's duplicating functionality, as ToolTipBehaviour already keeps track of the triggering location, unfortunately top secretly, as usual

扩展工具提示也无济于事

extending tooltip wouldn't help as well, due to the private scope of the behaviour

有什么想法吗?

public class DynamicTooltipMouseLocation extends Application {

    protected Button createButton(AnchorLocation location) {
        Tooltip t = new Tooltip("");
        String text = location != null ? location.toString() 
                : t.getAnchorLocation().toString() + " (default)";
        if (location != null) {
            t.setAnchorLocation(location);
        }
        t.setOnShown(e -> {
            // here we get a stable tooltip
            t.textProperty().set("x/y: " + t.getX() + "/" + t.getY() + "\n" +
                    "ax/y: " + t.getAnchorX() + "/" + t.getAnchorY());
        });
        Button button = new Button(text);
        button.setTooltip(t);
        button.setOnContextMenuRequested(e -> {
            LOG.info("context: " + text + "\n      " +
                    "scene/screen/source " + e.getSceneX() + " / " + e.getScreenX() + " / " + e.getX());
        });
        button.setOnMouseMoved(e -> {
            LOG.info("moved: " + text + "\n      " +
            "scene/screen/source " + e.getSceneX() + " / " + e.getScreenX() + " / " + e.getX());
        });
        return button;
    }

    @Override
    public void start(Stage stage) throws Exception {
        VBox pane = new VBox(createButton(AnchorLocation.CONTENT_TOP_LEFT));
        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(DynamicTooltipMouseLocation.class.getName());
}

推荐答案

我不确定我是否正确理解了您的问题,但是如果您正在寻找鼠标的屏幕坐标,请在显示了工具提示,我想您差不多可以了.

I'm not sure if I've understood your question right, but if you are looking for the screen coordinates of the mouse, right at the position when the tooltip is shown, I think you almost got them.

您已经看过Tooltip类及其内部类TooltipBehavior.

You have already looked at Tooltip class and its inner class TooltipBehavior.

对于初学者来说,这些硬编码的偏移量是:

For starters there are these hardcoded offsets:

private static int TOOLTIP_XOFFSET = 10;
private static int TOOLTIP_YOFFSET = 7;

然后,在内部类中,将一个鼠标移动的处理程序添加到该节点,以在屏幕坐标中跟踪鼠标,并基于多个计时器显示工具提示:

Then, in the inner class a mouse moved handler is added to the node, tracking the mouse in screen coordinates, and showing the tooltip based on several timers:

    t.show(owner, event.getScreenX()+TOOLTIP_XOFFSET,
                            event.getScreenY()+TOOLTIP_YOFFSET);

鉴于它使用此show方法:

public void show(Window ownerWindow, double anchorX, double anchorY)

您要寻找的坐标就是这些:

The coordinates you are looking for are just these:

coordMouseX=t.getAnchorX()-TOOLTIP_XOFFSET;
coordMouseY=t.getAnchorY()-TOOLTIP_YOFFSET;

无论如何设置工具提示锚点位置.

no matter how the tooltip anchor location is set.

我也在您对问题的回答中也进行了检查,这些值与设置为工具提示.

I've checked this also in your answer to the question, and these values are the same as the Point2D screen you set to the tooltip.

无论如何,由于此解决方案使用私有API中的硬编码字段,因此我认为您不喜欢它,因为这些内容可能会在不通知的情况下发生变化...

Anyway, since this solution uses hardcoded fields from private API, I assume you won't like it, since those can change without notice...

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

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