Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add hint text in a Textfield in JavaFX

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?

like image 466
user2556079 Avatar asked Dec 03 '15 14:12

user2556079


People also ask

How do you add a text field in JavaFX?

Creating a Text FieldLabel label1 = new Label("Name:"); TextField textField = new TextField (); HBox hb = new HBox(); hb. getChildren(). addAll(label1, textField); hb. setSpacing(10);

What is prompt text in JavaFX?

The TextField class in JavaFX is used to create a control that allows the user to enter in a single line of text. It supports having prompt text (i.e., text that informs the user what the TextField is meant to be used for). Note: If you need a multi-line text input control then have a look at the TextArea class.

How do you wrap text in TextField?

As a solution you can wrap the text within the width of the window by setting the value to the property wrapping with, using the setWrappingWidth() method. This method accepts a double value representing the width (in pixels) of the text.


1 Answers

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
like image 151
Emanuel Graf Avatar answered Oct 01 '22 11:10

Emanuel Graf