How to add hint text in a Textfield in JavaFX

user2556079 picture user2556079 · Dec 3, 2015 · Viewed 13.3k times · Source

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?

Answer

Emanuel Graf picture Emanuel Graf · Apr 10, 2018

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