JavaFX | Tooltip
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.
Commonly used methods:
|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.
Note: The above programs might not run in an online IDE please use an offline compiler.