How to set a tooltip on a JavaFX Button?

2019-02-22 06:11发布

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

3条回答
Ridiculous、
2楼-- · 2019-02-22 06:51

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

查看更多
欢心
3楼-- · 2019-02-22 06:51

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

<Button>
 <tooltip><Tooltip text="my tooltip" /></tooltip>
</Button>
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-22 06:53

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.

查看更多
登录 后发表回答