Alex Johnson
Alex Johnson

Reputation: 534

JavaFX: how to set tooltip location relative to the mouse cursor?

I have some Buttons, and I have added tooltips to them.

Button button = new Button("Example");
Tooltip tt = new Tooltip("example");
button.setTooltip(tt);

By default, when I mouse over the button, the tooltip pops up; however, the tooltip appears under the cursor. I am looking for a way to change the default location of the tooltip when moused over to something like 10px right and down. Something like below:

tooltip.setLocation(mouseLocation+10, mouseLocation-10);

I looked at JavaFX: how to set correct tooltip position? and it wasn't really what I was looking for because it is more about having the tooltip appear in relation to the screen.

Upvotes: 2

Views: 3533

Answers (3)

GOXR3PLUS
GOXR3PLUS

Reputation: 7255

Although the question is a little bit old,but remains unanswered.

  1. Part 1

What about something that is using Button current position in screen and set the tooltip position accordingly?

Button button = new Button();
button.setTooltip(new Tooltip("Hey i am a Button"));
button.getTooltip().setOnShowing(s->{

    //Get button current bounds on computer screen
    Bounds bounds = button.localToScreen(button.getBoundsInLocal());
    button.getTooltip().setX(bounds.getMaxX());
    button.getTooltip().setY(bounds.getMinY());

});
  1. Part 2

(You can do also with mouse but add somewhere a mouseMoveListener,in the example it is added on the Button but i would prefer to add it on the Stage Scene if we are speaking about a lot of Button)

    double currentMouseX;
    double currentMouseY;
    button.setOnMouseMoved(m->{
        currentMouseX=m.getScreenX();
        currentMouseY=m.getScreenY();
    });

    button.getTooltip().setOnShowing(s->{       
        button.getTooltip().setX(currentMouseX+button.getTooltip().getWidth()+5);
        button.getTooltip().setY(currentMouseY);                
    });

Finnaly:

Actually in the IDE you are using type button.getTooltip().setOn.. and you will see all the available methods that you can use to control tooltip before and after showing or hiding.

Upvotes: 3

George Forman
George Forman

Reputation: 580

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 char'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));
        });

This worked for me.

Upvotes: 0

Mansueli
Mansueli

Reputation: 6939

To get it relative to the mouse's position, you may use robot.

Example:

enter image description here

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;



public class Main extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        com.sun.glass.ui.Robot robot =
        com.sun.glass.ui.Application.GetApplication().createRobot();
        Pane pane = new Pane();
        Button btn = new Button("Stack");
        Tooltip tt = new Tooltip("example");
        tt.setX(robot.getMouseX()+10);
        tt.setY(robot.getMouseY()-10);
        btn.setTooltip(tt);
        pane.getChildren().add(btn);
        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args){
        launch(args);
    }
}

Upvotes: 0

Related Questions