How to set a tooltip on a JavaFX Button?

前端 未结 3 603
青春惊慌失措
青春惊慌失措 2021-01-07 21:47

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

相关标签:
3条回答
  • 2021-01-07 22:09

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

    <Button>
     <tooltip><Tooltip text="my tooltip" /></tooltip>
    </Button>
    
    0 讨论(0)
  • 2021-01-07 22: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.

    0 讨论(0)
  • 2021-01-07 22:17

    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 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);
    

    0 讨论(0)
提交回复
热议问题