JavaFX TextArea to ListView example

JavaFx User Interface Components: Exercise-19 with Solution

Write a JavaFX application with a TextArea and a button. When text is entered in the TextArea and the button is pressed, display the entered text in a ListView.

Sample Solution:

JavaFx Code:


import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text to ListView");

        // Create a TextArea for entering text.
        TextArea textArea = new TextArea();

        // Create a button for adding text to the ListView.
        Button addButton = new Button("Add to List");

        // Create a ListView to display entered text.
        ListView listView = new ListView<>();

        addButton.setOnAction(event -> {
            String enteredText = textArea.getText();
            if (!enteredText.isEmpty()) {

        // Create a layout to arrange the components.
        HBox inputBox = new HBox(10);
        inputBox.getChildren().addAll(textArea, addButton);

        VBox root = new VBox(10);
        root.getChildren().addAll(inputBox, listView);

        // Create the scene and set it in the stage.
        Scene scene = new Scene(root, 400, 300);

        // Set the title of the window.

In the above JavaFX application, you can input 'text' in the TextArea and click the "Add to List" button. The entered text will be displayed in the ListView below. The ListView dynamically updates with the entered text items.

Sample Output:

JavaFx: JavaFX TextArea to ListView example.
JavaFx: JavaFX TextArea to ListView example.


Flowchart: JavaFX TextArea to ListView example.

Java Code Editor:

Previous: JavaFX Download manager with ProgressBar.
Next: JavaFX to-do list application with ListView.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.