JavaFX Tooltip Tutorials

0
The JavaFX Tooltip is used to display additional information about the UI control(node) when the node is hovered over by the mouse.

The tooltip has two states: activated and showing.

Constructors :

  • Tooltip()Creates a tooltip with an empty string for its text.
  • Tooltip(String text)Creates a tooltip with the specified text.

Creating a Tooltip :

Tooltip tooltip = new Tooltip();
tooltip.setText("New Tooltip");

Set Tooltip for a node AS

node.setTooltip(tooltip);

Example of tool tip:


package javafxtuts;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

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

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label lb=new Label("Javafxtuts.com");
      Tooltip tooltip = new Tooltip();
      tooltip.setText("One of the best website for JavaFX tutorials");
      lb.setTooltip(tooltip);
      VBox root =new VBox();
      root.setPadding(new Insets(20, 10, 10, 10));
      root.getChildren().addAll(lb);
        Scene scene = new Scene(root, 250, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation :

  • Tooltip tooltip = new Tooltip();It create a new Tooltip.
  • To set the text to the tooltip use setText() AStooltip.setText("One of the best website for JavaFX tutorials");
  • lb.setTooltip(tooltip);It set tooltip to the label lb.
Share.

Leave A Reply