Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a tooltip to a node that does not implement Control using FXML?

In JavaFX subclasses of Control have a tooltip property which means that one can declaratively add a Tooltip using FXML thusly:

<CheckBox fx:id="someCheckBox" mnemonicParsing="false" text="Do It?">
    <tooltip>
        <Tooltip text="Whether or not to do it."/>
    </tooltip>
</CheckBox>

This is possible only because CheckBox is a subclass of Control.

However, I would like to be able to add a tooltip to any scene-graph Node using FXML (i.e. without having to programatically add the tooltip using Java).

like image 276
brcolow Avatar asked Apr 30 '16 03:04

brcolow


People also ask

How to add a tooltip in FXML?

To add a tooltip in FXML, you could also do this, <Button> <tooltip><Tooltip text="my tooltip" /></tooltip> </Button> Share Improve this answer Follow answered May 5 '17 at 4:02 ankur_kachruankur_kachru 45455 silver badges1313 bronze badges 4 1

How do I add tooltips to a node?

14 If you are using Scenebuilder, you can add tooltips by locating "Tooltip" under the Miscellaneous dropdown (Left panel) and dragging it to the node you want to install the tooltip. You can then specify various properties (Right panel) of the tooltip like style and text just as you would a normal Node.

How to add a tool tip to a form?

1 Add a ToolTip component to your form 2 Select one of the controls that you want a tool tip for 3 Open the property grid ( F4 ), in the list you will find a property called "ToolTip on toolTip1" (or something similar). 4 Repeat 2-3 for the other controls 5 Done. More ...

How to initialize tooltip on a single element in a container?

The following code example depicts the way to initialize Tooltip on a single element. It is possible to create Tooltip on multiple targets within a container. To do so, define the selector property with specific target elements - so that the tooltip will be initialized only on those matched targets within a container.


1 Answers

There is an archived discussion on the Oracle Community Forum that suggests wrapping the Node inside of a ScrollPane and setting the Tooltip on the ScrollPane. This is less than ideal though as it adds an unnecessary Node to the scene-graph but more importantly does not work well in all scenarios. Consider trying to add a Tooltip to the graphic of a TitledPane:

<TitledPane text="My TitledPane" fx:id="someTitledPane" expanded="true" contentDisplay="RIGHT">
    <graphic>
        <ScrollPane fitToWidth="true" fitToHeight="true" style="-fx-background-color: rgba(255, 255, 255, 0);">
            <ImageView fitWidth="24.0" fitHeight="24.0" preserveRatio="true" pickOnBounds="true">
            <image>
                <Image url="/images/questionMark.jpg" />
            </image>
            </ImageView>
        <tooltip>
            <Tooltip text="My tooltip."/>
        </tooltip>
        </ScrollPane>
    </graphic>
    <Label text="Hello!"/>
</TitledPane>

Even though the ScrollPane has a transparent background, there is still a visible white-box behind the question mark graphic:

ScrollPane leaves white background - not ideal!

There is a way around this, and that is to utilize the power of <fx:script>:

<?language javascript?>

<TitledPane text="My TitledPane" fx:id="someTitledPane" expanded="true" contentDisplay="RIGHT">
    <graphic>
        <ImageView fx:id="questionMarkImageView" fitWidth="24.0" fitHeight="24.0" preserveRatio="true" pickOnBounds="true">
        <image>
            <Image url="/images/questionMark.jpg" />
        </image>
        </ImageView>
        <fx:script>
            var tooltip = new javafx.scene.control.Tooltip('My tooltip');
            javafx.scene.control.Tooltip.install(questionMarkImageView, tooltip);
        </fx:script>
    </graphic>
    <Label text="Hello!"/>
</TitledPane>

Note that the fx:id of the ImageView is referenced inside of the script (I did not see this functionality documented anywhere and only found it through experimentation - which in fact prompted me to post this Q&A in the hope that others would find it useful). The results look like:

No white background and one less node in scene-graph - ideal!

like image 53
brcolow Avatar answered Oct 27 '22 00:10

brcolow