How to create a Text Area in JavaFX
Creating the JavaFX TextArea is very easy and simple because the text area in JavaFX is also a text input control and works the same as the JavaFX TextField and JavaFX PasswordField. The JavaFX TextArea is something like the advanced version of the TextField because the JavaFX TextField only allows the user to enter or write a single line plain text. Otherwise, the TextArea allows the user to enter or write a multiline plain text.
In this tutorial, you will learn a lot about how to use the text area in JavaFX. I will walk you through creating TextArea, will show you how to use the setter and getter of the node, and will also show you something new like the number of paragraphs, number of words, and number of characters. You will see the code in the given examples below.
JavaFX TextArea Examples
The first example will be creating the TextArea, adding or showing the TextArea to the Scene graph, and more examples below. You can also use Scene Builder to create the Graphical User Interface (GUI) easily. Otherwise, we will code it manually to create the JavaFX TextArea.
Create the TextArea
It is very easy to create the TextArea in JavaFX using its constructor. We need to create an instance of the TextArea to create this node. The following example below will show you how to create an instance of the JavaFX TextArea. The Text Area in JavaFX provides two types of constructor, the default one and with initial text.
// create a text area using the default constructor. TextArea txt1 = new TextArea(); // create a text area with an initial text. TextArea txt2 = new TextArea("JavaFX Tutorial");
Add to the Scene Graph
Adding the node to the Scene graph is important. This makes our node visible in our application. If you are using the Scene Builder to make your application, you can use the FXMLLoader to load your Scene. In this example, we will code it manually to the Java class. You will also need to know how to use the layout in JavaFX. The layout in JavaFX is something like a container. See the example code below to learn more about the layout and how to add our node to the scene.
// create a text area TextArea textArea = new TextArea(); //create a layout StackPane layout = new StackPane(textArea); // create a scene Scene scene = new Scene(layout, 400 ,400); stage.setScene(scene); stage.show();
Output
Setter and Getter in TextArea
These methods are the most used methods because developers will need to get the entered text from the user, or set an initial text to the TextArea in JavaFX. The following example code below will show you how to use the setText() and getText() methods.
// set a text to the TextArea TextArea textArea = new TextArea(); textArea.setText("This is a plain text"); // get the entered text of the TextArea TextArea textArea = new TextArea(); String data = textArea.getText();
Output
Wrap Text in TextArea in JavaFX
This method is also useful when it is needed. The setWrapText() method is a parameterized method. By default, its value is false. If you set it to true, the WrapText works if your paragraph or word reaches the other side of the TextArea, and it will automatically enter a new line. Please see the example code below and try it yourself to understand it better.
TextArea textArea = new TextArea(); textArea.setWrapText(true);
Set the Prompt Text
The word prompt tells you to do something. Use the setPromptText() method if you want to add a prompt text in your JavaFX TextArea something like “Enter your message here”. See the following example code below to learn more about the prompt text in JavaFX.
TextArea textArea = new TextArea(); textArea.setPromptText("Enter something here");
Output
Paragraph, word, and character counter
Some applications show this kind of feature. If you want to count the number of paragraphs, number of words, and number of characters in JavaFX. We can use the getParagraphs() method to get the number of paragraphs. You also need to use the split() method to get the number of words and use the getLength() method to get the number of characters. Please see the example code below to learn more about counting entered words in the TextArea in JavaFX.
// The event below is a key typed event private void textAreaTyped(KeyEvent event) { ObservableList<CharSequence> list = textArea.getParagraphs(); int par = list.size(); String[] words = textArea.getText().split("\\s+"); counter.setText("Paragraph: "+ par +" | Words: " + words.length + " | Characters: "+ textArea.getLength()); }