JavaFX:根据控件内容显示tooltip

105 篇文章 5 订阅
本文介绍了一个JavaFX应用程序如何在用户输入控件(如TextField)失去焦点时显示微帮助提示(Tooltip),并在控件有内容时隐藏。通过监听焦点变化并调整帮助文本的位置和可见性来提供实时帮助。
摘要由CSDN通过智能技术生成

如题。当控件为空,没有内容时显示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();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值