如题。当控件为空,没有内容时显示tooltip,反之不显示。示例如下:
package ch06;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* @copyright 2023-2022
* @package ch06
* @file MicroHelpApplication.java
* @date 2023-08-24 08:39
* @author qiao wei
* @version 1.0
* @brief
* @history
*/
public class MicroHelpApplication extends Application {
public static void main(String[] args) {
try {
Application.launch(MicroHelpApplication.class, args);
} catch (Exception exception) {
exception.printStackTrace();
}
}
public MicroHelpApplication() {}
@Override
public void start(Stage stage) throws Exception {
TextField firstNameField = new TextField();
TextField lastNameField = new TextField();
TextField salaryField = new TextField();
Button closeButton = new Button("Close");
closeButton.setOnAction(e -> Platform.exit());
firstNameField.getProperties().put("microHelpText", "Enter the first name");
lastNameField.getProperties().put("microHelpText", "Enter the last name");
salaryField.getProperties().put("microHelpText", "Enter a salary greater than $2000.00.");
// The help text node is unmanaged
helpText.setManaged(false);
helpText.setTextOrigin(VPos.TOP);
helpText.setFill(Color.RED);
helpText.setFont(Font.font(null, 9));
helpText.setMouseTransparent(true);
// Add all nodes to a GridPane
GridPane root = new GridPane();
root.add(new Label("First Name:"), 1, 1);
root.add(firstNameField, 2, 1);
root.add(new Label("Last Name:"), 1, 2);
root.add(lastNameField, 2, 2);
root.add(new Label("Salary:"), 1, 3);
root.add(salaryField, 2, 3);
root.add(closeButton, 3, 3);
root.add(helpText, 4, 3);
Scene scene = new Scene(root, 300, 100);
// Add a change listener to the scene, so you know when the focus owner
// changes and display the micro help
scene.focusOwnerProperty().addListener(
(ObservableValue<? extends Node> value,
Node oldNode,
Node newNode) -> focusChanged(value, oldNode, newNode)
);
stage.setScene(scene);
stage.setTitle("Showing Micro Help");
stage.show();
}
/**
* @class MicroHelpApplication
* @date 2023-08-24 08:58
* @author qiao wei
* @version 1.0
* @brief 控件获得焦点时触发。
* @param
* @return
* @throws
*/
public void focusChanged(ObservableValue<? extends Node> value,
Node oldNode,
Node newNode) {
// Focus has changed to a new node
String microHelpText = (String) newNode.getProperties().get("microHelpText");
if (microHelpText != null && microHelpText.trim().length() > 0) {
helpText.setText(microHelpText);
helpText.setVisible(true);
// Position the help text node
double x = newNode.getLayoutX() +
newNode.getLayoutBounds().getMinX() -
helpText.getLayoutBounds().getMinX();
double y = newNode.getLayoutY() +
newNode.getLayoutBounds().getMinY() +
newNode.getLayoutBounds().getHeight() -
helpText.getLayoutBounds().getMinX();
helpText.setLayoutX(x);
helpText.setLayoutY(y);
helpText.setWrappingWidth(newNode.getLayoutBounds().getWidth());
}
else {
helpText.setVisible(false);
}
}
// An instance variable to store the Text node reference
private Text helpText = new Text();
}