Javafx : Activate a tooltip with a button

后端 未结 3 1174
慢半拍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:20

    The following show a tooltip over control.
    If controlhas a Tooltip assigned to, this tooltip is not chnaged.

    public static void showOneTimeTooltip(Control control, String tooltipText) {
    
        Point2D p = control.localToScreen(5 , 5);
    
        final Tooltip customTooltip = new Tooltip(tooltipText);
        customTooltip.setAutoHide(false);
        customTooltip.show(control,p.getX(),p.getY());
    
        PauseTransition pt = new PauseTransition(Duration.millis(2000));
        pt.setOnFinished(e->{
            customTooltip.hide();
        });
        pt.play();
    }
    
    0 讨论(0)
  • The ability to display a Tooltip on demand requires a resolution of RT-19538 Customizable visibility timing for Tooltip, which is not implemented in JavaFX 2.2.

    As a workaround, you could try any of the possible strategies below:

    1. Displaying your Tooltip data in a ContextMenu instead. With a ContextMenu, you have complete control over when it is shown.
    2. You could create a custom PopupControl for your required functionality.
    3. You could replace the default TooltipSkin with a custom implemented skin which allows you to control when the Tooltip is displayed.
    4. You could implement RT-19538 and provide a patch to the Tooltip and TooltipSkin to the openjfx project.

    3rd party libraries such as Jide's JavaFX Beta Release provide special classes like Decorator utilities, IntelliHints and ShapedPopups which might be useful in your case.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题