JavaFX Tooltip Example

In this tutorial, we will learn how to use tooltip in the JavaFX application.

Any node can show a tooltip. Tooltip is a common UI element that is typically used for showing additional information about a node in the scene graph. It is shown when we hover a mouse pointer over a node.

JavaFX Tooltip Example

In the example, we set a tooltip to a button control:

package com.javaguides.javafx;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TooltipExample extends Application {

    @Override
    public void start(Stage stage) {
        HBox root = new HBox();
        root.setPadding(new Insets(20));

        Button btn = new Button("Button");
        btn.setMaxSize(100, 25);
        Tooltip tooltip = new Tooltip("Showing Tooltip");
        Tooltip.install(btn, tooltip);

        root.getChildren().add(btn);

        Scene scene = new Scene(root, 500, 350);

        stage.setTitle("Tooltip");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Let's understand the above JavaFX program.

A Button control is instantiated:

Button btn = new Button("Button");

A Tooltip is created and set to the button with the Tooltip's install() method:

Tooltiptooltip = new Tooltip("Button control");
Tooltip.install(btn, tooltip);

Output

Related JavaFX Examples

Comments