Raffaele Sassano
Raffaele Sassano

Reputation: 255

How to set a tooltip on a JavaFX Button?

How can I set a title or a text that appears above a button when I hover it with the mouse?

Upvotes: 24

Views: 33585

Answers (3)

DVarga
DVarga

Reputation: 21799

The Tooltip class is what you are looking for.

Example for a simple Tooltip

Button button = new Button("Hover Me");
button.setTooltip(new Tooltip("Tooltip for Button"));

enter image description here

You can also customize your Tooltips: CSS Reference for Tooltip.

Example for a styled Tooltip

Button button = new Button();
button.setText("Hover Me!");
Tooltip tt = new Tooltip();
tt.setText("Text on Hover");
tt.setStyle("-fx-font: normal bold 4 Langdon; "
    + "-fx-base: #AE3522; "
    + "-fx-text-fill: orange;");

button.setTooltip(tt);

enter image description here

Upvotes: 37

ankur_kachru
ankur_kachru

Reputation: 574

To add a tooltip in FXML, you could also do this,

<Button>
 <tooltip><Tooltip text="my tooltip" /></tooltip>
</Button>

Upvotes: 37

TM00
TM00

Reputation: 1390

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.

Upvotes: 17

Related Questions