How can I set a title or a text that appears above a button when I hover it with the mouse?
Asked
Active
Viewed 3.0k times
20
-
Do you want the text to appear on the Button or change the text of the Button? – ItachiUchiha May 31 '16 at 10:25
-
1Are you looking for a [Tooltip](https://docs.oracle.com/javafx/2/ui_controls/tooltip.htm) ? – Khaled SAB May 31 '16 at 10:27
3 Answers
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"));
You can also customize your Tooltip
s: 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);

DVarga
- 21,311
- 6
- 55
- 60
-
3Just 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
-
2I had to add this: For some reason, IntelliJ Idea didn't catch that one automatically. – Fran Marzoa Oct 14 '18 at 20:08
-
@FranMarzoa hmm IntelliJ does that for me, does Ctrl + Alt + O automatically add the import? – ankur_kachru Oct 15 '18 at 04:32
-
-
Note this only works for things that subclass control - see https://stackoverflow.com/questions/36950542/how-can-i-add-a-tooltip-to-a-node-that-does-not-implement-control-using-fxml – Mikeb May 11 '20 at 20:12
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