FreeCode

Donate HTML/CSS JavaFX Java Home
JavaFX Starter Template JavaFX Labels JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes JavaFX Choice Boxes JavaFX Combo Boxes JavaFX Text Areas JavaFX Text Fields JavaFX List Views JavaFX Image Views JavaFX Color Pickers JavaFX Date Pickers JavaFX Resizable Split Pane Layouts JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts JavaFX Web Views

JavaFX Text Areas

Code Description:

The code provided below shows how to create a Text Area in JavaFX. Text Areas are boxes where a user can enter free-form text. Text Areas are able to span multiple lines and can have line wrapping enabled or disabled.

Text Area Syntax:

TextArea textArea = new TextArea();

Runnable JavaFX Text Area Example Code:

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

public class Demo extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
		
	VBox root = new VBox();
		
	TextArea textArea = new TextArea();
	textArea.setWrapText(true);
	textArea.setPromptText("Type in stuff here...");
					
	root.getChildren().addAll(textArea);
		
	primaryStage.setTitle("Text Area Demo");
	primaryStage.setScene(new Scene(root, 200, 100));
	primaryStage.show();
		
    }

}

How to Run This Code:

Make sure that your Class name is the same as ours. In this case, we are using the Class name of "Demo". Alternatively, you can change the Class name in the provided code to match yours.

Note: You will NOT be able to copy and paste this code into your IDE unless you first set up JavaFX for your project. Check out this link if you need help setting up JavaFX:

JavaFX Comprehensive Setup Guide

Output:

JavaFX Text Area Output Image

Important Methods:

  • setText() - Used to set the current text of the text area.
  • getText() - Used to get the current text of the text area.
  • setWrapText() - Used to determine if the text will automatically wrap to the next line when out of space on the current one.
  • setPromptText() - Used to show example or temporary text when the text area is empty. After a user clicks in the text area, the prompt text will be removed.
  • setPrefSize() - Set the preferred width and height for the text area.
  • setFont() - Set the font and font size for the text area's text. EX: "textArea.setFont(new Font("Courier New", 20));"
  • setStyle() - Set an inline CSS-like style on the text area. EX: "textArea.setStyle("-fx-background-color: green;");"
  • setTooltip() - Set a message that displays when a user hovers over the text area for a certain amount of time. EX: "textArea.setTooltip(new Tooltip("Type Stuff!"));"
  • setDisable() - Used to make the text area grayed out and unclickable. EX: "textArea.setDisable(true);"
  • setVisible() - Show or hide a text area from the scene. EX: "textArea.setVisible(false);"

Related Links:

JavaFX Text Fields JavaFX Labels