Javafx : Activate a tooltip with a button

后端 未结 3 1199
慢半拍i
慢半拍i 2021-01-12 08:37

I\'m using JavaFx for a little app and a want to display a tooltip on a textArea when the user is clicking on a \"help\" button.

No problem for linking a tootltip to

3条回答
  •  甜味超标
    2021-01-12 09:32

    This is what you are looking for:

    final Button helpButton = new Button("Help");
    helpButton.setOnAction(new EventHandler()
    {
        public void handle(Event arg0)
        {
            showTooltip(stage, helpButton, "test tool tip", null);
        }
    });
    
    public static void showTooltip(Stage owner, Control control, String tooltipText,
        ImageView tooltipGraphic)
    {
        Point2D p = control.localToScene(0.0, 0.0);
    
        final Tooltip customTooltip = new Tooltip();
        customTooltip.setText(tooltipText);
    
        control.setTooltip(customTooltip);
        customTooltip.setAutoHide(true);
    
        customTooltip.show(owner, p.getX()
            + control.getScene().getX() + control.getScene().getWindow().getX(), p.getY()
            + control.getScene().getY() + control.getScene().getWindow().getY());
    
    }
    

    Just pass the button as an input instead of control.

提交回复
热议问题