20

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

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Raffaele Sassano
  • 215
  • 1
  • 2
  • 4

3 Answers3

33

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

DVarga
  • 21,311
  • 6
  • 55
  • 60
  • 3
    Just wanted to point out a tooltip takes 2 seconds of holding the mouse still until it appears. There's most probably a way to customize that time... – Egor Hans Feb 15 '18 at 17:08
  • 1
    @EgorHans Or maybe a way to show a hint that there is a tooltip for that button, like a small lightbulb – golimar May 13 '19 at 18:05
33

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

<Button>
 <tooltip><Tooltip text="my tooltip" /></tooltip>
</Button>
ankur_kachru
  • 534
  • 5
  • 13
16

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.

TM00
  • 1,330
  • 1
  • 14
  • 26