In this tutorial you will learn about tooltip via simple step by step examples.
Example 1: Simple ToolTip Example
Let us look at a simple tooltip example written in Java Programming Language. Follow the following steps:
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Add Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following Java files:
ToolTipExample.java
- In your editor or IDE, create a file known as
ToolTipExample.java
. - Then add the following code:
(a). ToolTipExample.java
Start by adding the following imports:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
Extend the Application class and invoke the launch()
function inside the main()
function:
public class ToolTipExample extends Application {
public static void main(String[] args) {
launch(args);
}
Override the start()
function passing in the primaryStage
and setting its title:
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX App");
Instantiate the ToolTip and pass in the Text property:
Tooltip tooltip1 = new Tooltip("Creates a new file");
You can set it’s minimum width as well as the Text alignment as shown below:
//tooltip1.setMinWidth(300);
tooltip1.setTextAlignment(TextAlignment.LEFT);
You can also set an ImageView onto the Tooltip using the setGraphic()
property as shown below:
tooltip1.setGraphic(new ImageView("file:iconmonstr-basketball-1-16.png"));
In this case we are binding the ToolTip to a simple button:
Button button1 = new Button("New");
button1.setTooltip(tooltip1);
Here is another example:
/*
Button button2 = new Button("Open");
Tooltip tooltip2 = new Tooltip("Opens an existing file from your local computer");
button2.setTooltip(tooltip2);
*/
Set the button to a HBox
, the set the Scene
as well and show Stage
:
HBox hBox = new HBox(button1);
//HBox hBox = new HBox(button1, button2);
Scene scene = new Scene(hBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Here is the full code:
package com.jenkov.javafx.tooltip;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class ToolTipExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX App");
Tooltip tooltip1 = new Tooltip("Creates a new file");
//tooltip1.setMinWidth(300);
tooltip1.setTextAlignment(TextAlignment.LEFT);
tooltip1.setGraphic(new ImageView("file:iconmonstr-basketball-1-16.png"));
Button button1 = new Button("New");
button1.setTooltip(tooltip1);
/*
Button button2 = new Button("Open");
Tooltip tooltip2 = new Tooltip("Opens an existing file from your local computer");
button2.setTooltip(tooltip2);
*/
HBox hBox = new HBox(button1);
//HBox hBox = new HBox(button1, button2);
Scene scene = new Scene(hBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Step 4: Run
Copy the above code, add static main method
and from it instantiate the above class and run.
Reference
Here are the reference links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |