java - Tooltip:如何获取触发提示的鼠标坐标?

标签 java tooltip javafx-8

需求:将触发工具提示的鼠标事件的坐标显示为其文本。对于 contextMenu,位置存储在 contextMenuEvent 中,因此我会监听 contextMenuRequested 并根据需要进行更新。

找不到任何类似的工具提示,所以玩了一下(参见下面的示例):

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

  • 蛮力方法是 install a mouse-moved handler并将当前鼠标位置存储到工具提示的属性中。不想这样做,因为这是重复的功能,因为 ToolTipBehaviour 已经跟踪触发位置,不幸的是,像往常一样,这是 secret 的

  • 由于行为的私有(private)范围,扩展工具提示也没有帮助

有什么想法吗?

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());
}

最佳答案

我不确定我是否正确理解了你的问题,但如果你正在寻找鼠标的屏幕坐标,就在显示工具提示的位置,我想你几乎得到了它们。

您已经查看了 Tooltip 类及其内部类 TooltipBehavior

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

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

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

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

鉴于它使用此 show 方法:

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

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

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

无论工具提示 anchor 位置如何设置。

我也在您对 question 的回答中检查了这一点,这些值与您为工具提示设置的 Point2D 屏幕 相同。

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

关于java - Tooltip:如何获取触发提示的鼠标坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28585330/

相关文章:

javascript - 当按钮在 HTML 中处于事件状态时向按钮添加提示

javascript - Bootstrap 工具提示 : how to specify tooltip text using a javascript function

charts - JavaFX 图表 - CategoryAxis 调整大小问题

Java可视化库称为 "C-BOMB"之类的?我想我听错了

html - 你如何在禁用的 HTML 按钮上呈现工具提示

javafx-8 - 从另一个类启动 JavaFX 应用程序

JavaFX8 禁用 ScrollPane 的滚动

java - android adMob 找不到类

java - Eclipse 致力于 SVN

java - IIB - 使用 Java 计算节点将 BLOB 转换为字符串