JavaFX Tooltip Example

In this JavaFX example, we will show you how to use tooltip in the JavaFX application.

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

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

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

Output


Comments