如何在JavaFX的Textfield中添加提示文本

问题描述 投票:7回答:1

我想在文本字段中添加一些提示文本,例如“name”或“surname”。我创建像这个TextField userTextField = new TextField();的文本域,但我找不到如何做到这一点。在这里,我刚发现这个Clear prompt text in JavaFX TextField only when user starts typing,但我不敢相信这是唯一的方法。或者我错了?

text javafx textfield hint
1个回答
5
投票

我是这样做的:

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
© www.soinside.com 2019 - 2024. All rights reserved.