How to display a tooltip according to mouse position? - JavaFX

我与影子孤独终老i 提交于 2019-12-22 10:38:34

问题


I have a stackPane, filled with a Circle and a couple of lines.

I want to display a tooltip while hovering over the StackPane and the tooltip should contain the X/Y coords of the mouse.

I know how to get the Coords of the mouse, but I'm unable to find a way of showing the tool tip.

Can any of ou guys help me with that?..


回答1:


try this...

Tooltip tp = new Tooltip("at stack tool");
stackpane.setOnMouseEntered(new EventHandler<MouseEvent>() {
     @Override
     public void handle(MouseEvent t) {
          Node  node =(Node)t.getSource();
          tp.show(node, FxApp.stage.getX()+t.getSceneX(), FxApp.stage.getY()+t.getSceneY());
        }
    });



回答2:


Anshul Parashar's answer probably works, but ToolTip also has a 'installation' static helper method to handle display on hover.

Assuming n is a Node:

Tooltip tp = new Tooltip("at stack tool");
Tooltip.install(n, tp);



回答3:


I solved it like this:

    Tooltip mousePositionToolTip = new Tooltip("");
    gridPane.setOnMouseMoved(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            String msg = "(x: " + event.getX() + ", y: " + event.getY() + ")\n(sceneX: "
                    + event.getSceneX() + ", sceneY: " + event.getSceneY() + ")\n(screenX: "
                    + event.getScreenX() + ", screenY: " + event.getScreenY() + ")";
            mousePositionToolTip.setText(msg);

            Node node = (Node) event.getSource();
            mousePositionToolTip.show(node, event.getScreenX() + 50, event.getScreenY());
        }

    });

It will show a ToolTip in right of your mouse-pointer. You can replace your StackPane with gridPane in my code and it should work. But i didn't test it.




回答4:


The prior solution is OK, but it gets called for every mouse movement.

Instead, here's a solution that just gets called once when it's about to display the tooltip:

The JavaFx 8 Tooltip provides event callbacks just before and after the tooltip displays (and just before and after it's taken down). So, install an event handler for the "just before" call like this below. The window event doesn't give you the current mouse coordinates, unfortunately, but you can still get them at any time with java.awt.MouseInfo.getPointerInfo().getLocation() as below.

Tooltip t = new Tooltip();
Tooltip.install(yournode, t);
t.setOnShowing(ev -> {// called just prior to being shown
    Point mouse = java.awt.MouseInfo.getPointerInfo().getLocation();
    Point2D local = yournode.screenToLocal(mouse.x, mouse.y);

    // my app-specific code to get the chart's yaxis value
    // then set the text as I want
    double pitch = yaxis.getValueForDisplay(local.getY()).doubleValue();
    double freq = AudioUtil.pitch2frequency(pitch);
    t.setText(String.format("Pitch %.1f:  %.1f Hz   %.1f samples", pitch, freq, audio.rate / freq));
});

Works for me.



来源:https://stackoverflow.com/questions/21159182/how-to-display-a-tooltip-according-to-mouse-position-javafx

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