JavaFX - TextArea



The TextArea control is a graphical user interface component that allow users to enter and display multiple lines of plain text. It is mostly used to collect information like comments, feedbacks and descriptions. In the figure below, we can see a text area with some pre-defined texts −

JavaFX Textarea

Creating TextArea in JavaFX

In JavaFX, the text area is represented by a class named TextArea which is a part of javafx.scene.control package. By instantiating this class, we can create a text area in JavaFX. Its constructors are listed below −

  • TextArea() − It is the default constructor that constructs a text area without any texts.

  • TextArea(String str) − It constructs a new text area with the predefined text.

Properties of JavaFX TextArea

Once the TextArea is created, it can be customized to enhance its appearance and behavior using its properties. For instance, we can set the preferred number of rows and columns using the prefRowCount and prefColumnCount properties respectively. Additionally, we can also enable or disable text wrapping using the wrapText property.

The TextArea also supports showing prompt text when there is no text in the component. This is a useful way of informing the user what is expected in the text area, without using tooltips or labels. The prompt text can be set using the setPromptText() method or the promptText property.

Example

In the following example, we are going to create a TextArea in JavaFX application. Save this code in a file with the name JavafxTextarea.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class JavafxTextarea extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label label = new Label("Try typing Text in the box...");
      // Creating a TextArea with fixed size
      TextArea txtArea = new TextArea();
      // Setting the preferred size
      txtArea.setPrefSize(200, 200); 
      // Enabling text wraps property
      txtArea.setWrapText(true);
      // Create a HBox and add the Label and TextArea to it
      HBox box = new HBox(label, txtArea);
      box.setAlignment(Pos.BASELINE_CENTER);
      box.setSpacing(10);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(box, 400, 300);
      stage.setScene(scene);
      // Set the title of the Stage
      stage.setTitle("TextArea in JavaFX");
      // Display the Stage
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media JavafxTextarea.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.media JavafxTextarea

Output

When we execute the above code, it will generate the following output −

Textarea Output

Creating TextArea using its parameterized Constructor

The TextArea is created either by using its default constructor or parameterized constructor. One benefit of using parameterized constructor over default constructor is that we can provide an additional text to indicate expected input.

Example

The following JavaFX code demonstrates how to use the parameterized constructor of the TextArea class to create a text area within an application. Save this code in a file with the name TextareaDemo.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
public class TextareaDemo extends Application {
   @Override
   public void start(Stage stage) {
      // Create a TextArea
      TextArea txtArea = new TextArea("Try typing Text in the box...");
      // Create a Button to clear the TextArea
      Button clearButton = new Button("Clear Text");
      // setting action
      clearButton.setOnAction(e -> txtArea.clear());
      // Create a VBox and add the TextArea and Button to it
      VBox vbox = new VBox(txtArea, clearButton); 
      vbox.setSpacing(10);
      // Apply CSS styling
      vbox.setStyle("-fx-padding: 10;" +
         "-fx-border-style: solid inside;" +
         "-fx-border-width: 2;" +
         "-fx-border-insets: 5;" +
         "-fx-border-radius: 5;" +
         "-fx-border-color: blue;");
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(vbox, 400, 300);
      stage.setScene(scene);
      // Set the title of the Stage
      stage.setTitle("TextArea in JavaFX");
      // Display the Stage
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls TextareaDemo.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextareaDemo

Output

On executing the above code, it will generate the following output. When we click on the clear text button, all text inside the textarea will be deleted.

Textarea Output2
Advertisements