JavaFX Tooltip

A JavaFX Tooltip displays a small popup with supporting text when the pointer pauses over a user-interface element. Tooltips are commonly attached to controls such as Button, Hyperlink, TextField, and CheckBox.

This tutorial shows how to create a JavaFX Tooltip, attach it to controls, change its text and timing, enable text wrapping, and install a tooltip on a JavaFX node that is not a Control.

Create and Attach a JavaFX Tooltip

To create a JavaFX Tooltip, pass the popup text to the Tooltip constructor.

</>
Copy
 Tooltip tooltip = new Tooltip("Some text");

Import javafx.scene.control.Tooltip in the Java class that creates the tooltip.

</>
Copy
import javafx.scene.control.Tooltip;

For a JavaFX control, call its setTooltip() method.

</>
Copy
control.setTooltip(tooltip);

JavaFX Tooltip Example for a Button

The following program creates a Button, creates a Tooltip with the text Creates a new file, and assigns that tooltip to the button.

JavaFxTooltipTutorial.java

</>
Copy
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.control.Button;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxTooltipTutorial extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("JavaFX Tooltip - tutorialkart.com");
            
            //javafx tooltip
            Tooltip tooltip = new Tooltip("Creates a new file");
            
            //javafx button
            Button button = new Button("Upload");
            //set tooltip for button
            button.setTooltip(tooltip);
            
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add button to the tile pane
            tilePane.getChildren().add(button);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run the application. The initial window contains the button shown below.

JavaFX Tooltip - Button without hover

Move the pointer over the button and pause briefly. JavaFX displays the assigned Tooltip near the pointer.

JavaFX Tooltip - When user hovers on button

JavaFX Tooltip Example for a Hyperlink

This example assigns a Tooltip to a JavaFX Hyperlink. The tooltip provides additional information without changing the text displayed by the hyperlink itself.

JavaFxTooltipTutorial2.java

</>
Copy
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxTooltipTutorial2 extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("JavaFX Tooltip - tutorialkart.com");
            
            //javafx tooltip
            Tooltip tooltip = new Tooltip("Best Online Tutorials.");
            
            //javafx hyperlink
            Hyperlink hyperlink = new Hyperlink("www.tutorialkart.com");
            //set tooltip for hyperlink
            hyperlink.setTooltip(tooltip);
            
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add hyperlink to the tile pane
            tilePane.getChildren().add(hyperlink);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run the application and pause the pointer over the hyperlink. The Tooltip appears as a popup containing the assigned text.

JavaFX Tooltip to Hyperlink

Change JavaFX Tooltip Text After Creation

The Tooltip text can be read with getText() and updated with setText(). This is useful when the description depends on the current state of the application.

</>
Copy
Tooltip tooltip = new Tooltip("Upload a file");
Button button = new Button("Upload");
button.setTooltip(tooltip);

// Update the same tooltip later.
tooltip.setText("Upload is currently unavailable");

Wrap Long Text in a JavaFX Tooltip

A long tooltip can become difficult to read if it remains on one line. Set a preferred width and enable wrapping so the text is displayed across multiple lines.

</>
Copy
Tooltip tooltip = new Tooltip(
        "Choose a file from your computer and upload it to the application."
);
tooltip.setPrefWidth(260);
tooltip.setWrapText(true);

Configure JavaFX Tooltip Show and Hide Delays

JavaFX provides delay properties for controlling when a Tooltip appears, how long it remains visible, and how quickly it disappears after the pointer leaves the target. These values use javafx.util.Duration.

</>
Copy
import javafx.scene.control.Tooltip;
import javafx.util.Duration;

Tooltip tooltip = new Tooltip("Open settings");
tooltip.setShowDelay(Duration.millis(300));
tooltip.setShowDuration(Duration.seconds(5));
tooltip.setHideDelay(Duration.millis(150));

Use timing values that give users enough time to discover and read the description. Avoid very short durations for tooltips that contain more than a few words.

Install a JavaFX Tooltip on a Non-Control Node

Classes such as Button and Hyperlink inherit the setTooltip() method from Control. A general JavaFX Node, such as a Rectangle, does not have that method. Use the static Tooltip.install() method instead.

</>
Copy
import javafx.scene.control.Tooltip;
import javafx.scene.shape.Rectangle;

Rectangle rectangle = new Rectangle(120, 50);
Tooltip tooltip = new Tooltip("Preview area");

Tooltip.install(rectangle, tooltip);

To remove a Tooltip installed this way, call Tooltip.uninstall(node, tooltip) with the same node and Tooltip instance.

</>
Copy
Tooltip.uninstall(rectangle, tooltip);

JavaFX Tooltip Usage Notes

  • Keep Tooltip text brief and describe the control’s purpose or result.
  • Do not use a Tooltip as the only place for essential instructions, validation errors, or required actions.
  • Avoid placing interactive buttons or links inside a Tooltip; use a dialog, popover, or another visible control for interactive content.
  • Use consistent wording and timing across related controls.
  • Test whether each Tooltip remains readable when the application uses larger fonts or a narrow window.

JavaFX Tooltip Questions

How do I add a Tooltip to a JavaFX Button?

Create a Tooltip and pass it to button.setTooltip(tooltip). JavaFX then displays the popup when the pointer pauses over the button.

Can the same JavaFX Tooltip be reused?

A Tooltip instance can be assigned where appropriate, but separate instances are usually clearer when controls need different text or independent state. Updating a shared Tooltip also changes the text seen from every place that uses that instance.

Why does a JavaFX Node not have setTooltip()?

setTooltip() belongs to the JavaFX Control class. For another type of Node, attach the Tooltip with Tooltip.install(node, tooltip).

How do I make a JavaFX Tooltip stay visible longer?

Call setShowDuration() with a Duration, such as Duration.seconds(8). You can also adjust setShowDelay() and setHideDelay().

JavaFX Tooltip Tutorial Summary

In this JavaFX Tutorial, we created Tooltip objects, attached them to a Button and Hyperlink, updated Tooltip text, wrapped long descriptions, configured display timing, and installed a Tooltip on a non-Control node.