How to add hint text in a Textfield in JavaFX

∥☆過路亽.° 提交于 2020-12-08 06:13:20

问题


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?


回答1:


I do it like this:

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


来源:https://stackoverflow.com/questions/34069030/how-to-add-hint-text-in-a-textfield-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!