Skip to content
Related Articles

Related Articles

Improve Article

JavaFX | Tooltip

  • Last Updated : 24 Oct, 2019
Geek Week

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:

  1. Tooltip(): Creates a tooltip with an empty string for its text.
  2. Tooltip(String t): Creates a tooltip with the specified text.

Commonly used methods:

methodexplanation
getFont()Gets the value of the property font.
getText()Gets the value of the property text.
getTextAlignment()Gets the value of the property textAlignment.
install(Node n, Tooltip t)Associates the given Tooltip with the given Node.
isActivated()Gets the value of the property activated.
setFont(Font v)Sets the value of the property font.
setText(String v)Sets the value of the property text.
setTextAlignment(TextAlignment v)Sets the value of the property textAlignment.

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 labels
import 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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :