Tooltip class is used to define a tool tip control. When a mouse hovers on a control, tool tip displays additional information.
Can a Tooltip have Graphic icon?
Yes
Tooltip class provides following constructors to define Tooltip instance.
public Tooltip()
Creates a tooltip with an empty string for its text.
public Tooltip(String text)
Creates a tooltip with the specified text.
Example
Tooltip toolTip = new Tooltip("Enter registered username");
toolTip.setFont(Font.font("Verdana", 15));
userNameTextField.setTooltip(toolTip);
Find the below working application.
tooltipDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.TooltipDemoController"
fx:id="gridPane1"
hgap="20" vgap="20"
alignment="top_center">
<padding>
<Insets top="20" right="20" bottom="10" left = "30" />
</padding>
<Label fx:id="userNameLabel" text="Enter user name"
GridPane.rowIndex="0"
GridPane.columnIndex="0"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Label>
<Label fx:id="passwordLabel" text="Enter password"
GridPane.rowIndex="1"
GridPane.columnIndex="0"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Label>
<TextField fx:id="userNameTextField"
GridPane.rowIndex="0"
GridPane.columnIndex="1" />
<TextField fx:id="passwordTextField"
GridPane.rowIndex="1"
GridPane.columnIndex="1"
/>
<Button fx:id="signInButton" text="Sign In"
GridPane.rowIndex="2"
GridPane.columnIndex="0"
onAction="#loginAction"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Button>
<Button fx:id="clearButton" text="Clear"
GridPane.rowIndex="2"
GridPane.columnIndex="1"
onAction="#clearAction"
>
<font>
<Font name="Verdana" size="20" />
</font>
</Button>
<Label fx:id="resultLabel" text=""
GridPane.rowIndex="3"
GridPane.columnIndex="0"
GridPane.columnSpan="2"
>
<font>
<Font name="Verdana" size="35" />
</font>
<effect>
<DropShadow offsetX="3" offsetY="3">
<color>
<Color red="1" green="0" blue="0" opacity="0.7"/>
</color>
</DropShadow>
</effect>
</Label>
</GridPane>
TooltipDemoController.java
package com.sample.app.widgets.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.text.Font;
public class TooltipDemoController implements Initializable {
@FXML
private Label userNameLabel;
@FXML
private Label passwordLabel;
@FXML
private Label resultLabel;
@FXML
private TextField userNameTextField;
@FXML
private TextField passwordTextField;
@FXML
private Button signInButton;
@FXML
private Button clearButton;
@FXML
private void loginAction() {
String userName = userNameTextField.getText();
String password = passwordTextField.getText();
if (password == null || userName == null || userName.trim().isEmpty() || password.trim().isEmpty()) {
resultLabel.setText("Input shouldn't be empty");
return;
}
if (userName.equals("krishna") && password.equals("krishna")) {
resultLabel.setText("Welcome Krishna");
return;
}
resultLabel.setText("Wrong Credentials");
}
@FXML
private void clearAction() {
resultLabel.setText("");
userNameTextField.clear();
passwordTextField.clear();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
Tooltip userNameTooltip = new Tooltip("Enter registered username");
userNameTooltip.setFont(Font.font("Verdana", 15));
Tooltip passwordTooltip = new Tooltip("Enter password");
passwordTooltip.setFont(Font.font("Verdana", 15));
userNameTextField.setTooltip(userNameTooltip);
passwordTextField.setTooltip(passwordTooltip);
}
}
TooltipDemo.java
package com.sample.app.widgets;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TooltipDemo extends Application {
private static final String FXML_FILE = "/tooltipDemo.fxml";
private static final String STAGE_TITLE = "Tooltip Demo";
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));
Scene scene = new Scene(root, 700, 400, Color.WHITE);
primaryStage.setTitle(STAGE_TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
Hover mouse on text fields, you will see respective tooltip message.
Previous Next Home
No comments:
Post a Comment