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):
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
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
Any ideas?
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());
}