JavaFX | Tooltip - GeeksforGeeks

24 Oct, 2019
Tooltip is a part of JavaFx package. Tooltip is used to show additional information to the user when the mouse is hovered over the component. All the components can be associated with a tooltip and it can be also associated with a part of screen.
Constructors of the Tooltip class:
Tooltip(): Creates a tooltip with an empty string for its text.Tooltip(String t): Creates a tooltip with the specified text.
Tooltip(): Creates a tooltip with an empty string for its text.
Tooltip(String t): Creates a tooltip with the specified text.
Commonly used methods:
Below program illustrate the use of Tooltip in Java:
Program to create a label and add Tooltip text to the labels: We will create 3 tooltip objects t, t1, t2 for labels l, l1 l2. Then we will set the font for two labels t1 and t2. For t we will set Arial font and for t1 Tooltip we will set Verdana font. We will set the textAlignment for text of t1 and t2 to Left and Right respectively and we can set the tooltip to a label using two ways: The first is by using setTooltip() function and other using install() function. After that a Tile-pane is created, on which addChildren() method is called to attach the labels inside the scene, along with the resolution specified by (200, 200) in the code. Finally the show() method is called to display the final results.
// Java program to create label and add Tooltip text to the labelsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.collections.*;import javafx.stage.Stage;import javafx.scene.text.Text.*;import javafx.scene.text.*;public class tooltip extends Application {    // labels    Label l, l1, l2;      // tooltip    Tooltip t, t1, t2;      // launch the application    public void start(Stage s)    {        // set title for the stage        s.setTitle("creating Tooltip");          // create a tile pane        TilePane r = new TilePane();          // create a label        l = new Label("This is a label 1 ");        l1 = new Label("This is a label 2 ");        l2 = new Label("This is a label 3 ");          // create tooltip for labels        t = new Tooltip();        t1 = new Tooltip("tooltip for label 2");        t2 = new Tooltip("tooltip for label 3");          // set text for label 1        t.setText("tooltip for label 1");          // set font for tooltip        t.setFont(Font.font("Arial", FontPosture.ITALIC, 15));        t1.setFont(Font.font("Verdana", FontPosture.REGULAR, 10));          // set alignment for tooltip text        t1.setTextAlignment(TextAlignment.LEFT);        t2.setTextAlignment(TextAlignment.RIGHT);          // set the tooltip for labels        l.setTooltip(t);        l1.setTooltip(t1);        Tooltip.install(l2, t2);          // add label        r.getChildren().add(l);        r.getChildren().add(l1);        r.getChildren().add(l2);          // create a scene        Scene sc = new Scene(r, 200, 200);          // set the scene        s.setScene(sc);          s.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Tooltip.html
ManasChhabra2
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Generics in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java