如何在JavaFX的文本字段中添加提示文本 [英] How to add hint text in a Textfield in JavaFX

查看:83
本文介绍了如何在JavaFX的文本字段中添加提示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本字段中添加一些提示文本,例如"name"或"surname".我像这样创建文本字段TextField userTextField = new TextField();,但是我找不到如何做到这一点.在这里,我发现了这个仅当用户开始键入时,才清除JavaFX TextField中的提示文本,但我不相信这是唯一的方法.还是我错了?

I want to add some hint text in a textfield, like "name" or "surname". I create the textfield like this TextField userTextField = new TextField(); , but I cannot find how to do that. Here, I just found this Clear prompt text in JavaFX TextField only when user starts typing but I cannot believe this is the only way to do it. Or I am wrong?

推荐答案

我这样做是这样的:

userTextField.setPromptText("name"); //to set the hint text
userTextField.getParent().requestFocus(); //to not setting the focus on that node so that the hint will display immediately

这篇关于如何在JavaFX的文本字段中添加提示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆