问题
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