JavaFX TextArea

Jakob Jenkov
Last update: 2016-05-19

A JavaFX TextArea control enables users of a JavaFX application to enter text spanning multiple lines, which can then be read by the application. The JavaFX TextArea control is represented by the class javafx.scene.control.TextArea .

Creating a TextArea

You create a TextArea control by creating an instance of the TextArea class. Here is a JavaFX TextArea instantiation example:

TextArea textArea = new TextArea();

Adding a TextArea to the Scene Graph

For a JavaFX TextArea to be visible the TextArea object must be added to the scene graph. This means adding it to a Scene object, or as child of a layout which is attached to a Scene object.

Here is an example that attaches a JavaFX TextArea to the scene graph:

package com.jenkov.javafx.controls;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class TextAreaExperiments extends Application  {


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("TextArea Experiment 1");

        TextArea textArea = new TextArea();

        VBox vbox = new VBox(textArea);

        Scene scene = new Scene(vbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

The result of running the above JavaFX TextArea example is an application that looks like this:

A JavaFX TextArea component displayed in the scene graph.

Reading the Text of a TextArea

You can read the text entered into a TextArea via its getText() method. Here is an example of reading text of a JavaFX TextArea control via its getText() method:

String text = textArea.getText();

Here is a full example that shows a TextArea and a Button and which reads the text entered into the TextArea when the button is clicked:

package com.jenkov.javafx.controls;

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;


public class TextAreaExperiments extends Application  {


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("TextArea Experiment 1");

        TextArea textArea = new TextArea();

        Button button = new Button("Click to get text");
        button.setMinWidth(50);

        button.setOnAction(action -> {
            System.out.println(textArea.getText());

            textArea.setText("Clicked!");
        });

        VBox vbox = new VBox(textArea, button);

        Scene scene = new Scene(vbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

Setting the Text of a TextArea

You can set the text of a TextArea control via its setText() method. Here is an example of setting the text of a TextArea control via setText() :

textArea.setText("New Text");

Jakob Jenkov

Featured Videos




















Advertisements

High-Performance
Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next