- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX Installation and Architecture
- JavaFX - Installation Using Netbeans
- JavaFX - Installation Using Eclipse
- JavaFX - Installation using Visual Studio Code
- JavaFX - Architecture
- JavaFX - Application
- JavaFX 2D Shapes
- JavaFX - 2D Shapes
- JavaFX - Drawing a Line
- JavaFX - Drawing a Rectangle
- JavaFX - Drawing a Rounded Rectangle
- JavaFX - Drawing a Circle
- JavaFX - Drawing an Ellipse
- JavaFX - Drawing a Polygon
- JavaFX - Drawing a Polyline
- JavaFX - Drawing a Cubic Curve
- JavaFX - Drawing a Quad Curve
- JavaFX - Drawing an Arc
- JavaFX - Drawing an SVGPath
- JavaFX Properties of 2D Objects
- JavaFX - Stroke Type Property
- JavaFX - Stroke Width Property
- JavaFX - Stroke Fill Property
- JavaFX - Stroke Property
- JavaFX - Stroke Line Join Property
- JavaFX - Stroke Miter Limit Property
- JavaFX - Stroke Line Cap Property
- JavaFX - Smooth Property
- Operations on 2D Objects
- JavaFX - 2D Shapes Operations
- JavaFX - Union Operation
- JavaFX - Intersection Operation
- JavaFX - Subtraction Operation
- JavaFX Color and Texture
- JavaFX - Colors
- JavaFX Text
- JavaFX - Text
- JavaFX Effects
- JavaFX - Effects
- JavaFX Transformations
- JavaFX - Transformations
- JavaFX Animations
- JavaFX - Animations
- JavaFX Images
- JavaFX - Images
- JavaFX 3D Shapes
- JavaFX - 3D Shapes
- JavaFX - Creating a Box
- JavaFX - Creating a Cylinder
- JavaFX - Creating a Sphere
- Properties of 3D Objects
- JavaFX - Cull Face Property
- JavaFX - Drawing Modes Property
- JavaFX - Material Property
- JavaFX Event Handling
- JavaFX - Event Handling
- JavaFX - Using Convenience Methods
- JavaFX - Event Filters
- JavaFX - Event Handlers
- JavaFX UI Controls
- JavaFX - UI Controls
- JavaFX - ListView
- JavaFX - Accordion
- JavaFX - ButtonBar
- JavaFX - ChoiceBox
- JavaFX - HTMLEditor
- JavaFX - MenuBar
- JavaFX - Pagination
- JavaFX - ProgressIndicator
- JavaFX - ScrollPane
- JavaFX - Separator
- JavaFX - Slider
- JavaFX - Spinner
- JavaFX - SplitPane
- JavaFX - TableView
- JavaFX - TabPane
- JavaFX - ToolBar
- JavaFX - TreeView
- JavaFX - Label
- JavaFX - CheckBox
- JavaFX - RadioButton
- JavaFX - TextField
- JavaFX - PasswordField
- JavaFX - FileChooser
- JavaFX - Hyperlink
- JavaFX - Tooltip
- JavaFX - Alert
- JavaFX - DatePicker
- JavaFX - TextArea
- JavaFX Charts
- JavaFX - Charts
- JavaFX - Creating Pie Chart
- JavaFX - Creating Line Chart
- JavaFX - Creating Area Chart
- JavaFX - Creating Bar Chart
- JavaFX - Creating Bubble Chart
- JavaFX - Creating Scatter Chart
- JavaFX - Creating Stacked Area Chart
- JavaFX - Creating Stacked Bar Chart
- JavaFX Layout Panes
- JavaFX - Layout Panes
- JavaFX - HBox Layout
- JavaFX - VBox Layout
- JavaFX - BorderPane Layout
- JavaFX - StackPane Layout
- JavaFX - TextFlow Layout
- JavaFX - AnchorPane Layout
- JavaFX - TilePane Layout
- JavaFX - GridPane Layout
- JavaFX - FlowPane Layout
- JavaFX CSS
- JavaFX - CSS
- Media with JavaFX
- JavaFX - Playing Video
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
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 −
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 −
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.