Search code examples
javajavafxtooltipscenebuilder

Set custom duration for tooltips in JAVAFX


I'm looking for a solution for the following problem: I've built up a javafx GUI with SceneBuilder and added tooltips to some of the labels I added.

However, the tooltips automatically hide after ~5 seconds. Sometimes this is not enough for the user to read the tooltips whole content. I would like to show the tooltip as long as the cursor stays above the label and completely disable this autoHide function.

I did not find a way to customize the time a popup is shown or how to disable the auto hide function completely. Has somebody solved this or a similar problem?

Thanks in advance!


Solution

  • In JavaFX 9 you can set the showDuration (and showDelay) property:

    tooltip.setShowDuration(Duration.seconds(10));
    

    or in FXML

    <Tooltip text="Some text">
        <showDuration>
            <Duration millis="10000" />
        </showDuration>
    </Tooltip>
    

    You can also configure this using CSS: the following

    .tooltip {
        -fx-show-duration: 10s ;
    }
    

    in an external CSS file will set the show duration to 10 seconds for all tooltips. (And obviously you can set style classes and/or ids on the tooltip to create more specific CSS selectors.)

    There is no API for this in earlier versions of JavaFX.