Skip to main content

Overview

JTextArea is a customized JavaFX TextArea with built-in styling and validation support. It extends TextArea and provides additional methods for styling, validation states, and label creation. Perfect for multi-line text input like comments, descriptions, or messages.

Constructor

JTextArea()
constructor
Creates a new JTextArea with no prompt text.
JTextArea textArea = new JTextArea();
JTextArea(String promptText)
constructor
Creates a new JTextArea with the specified prompt text.
JTextArea textArea = new JTextArea("Enter your comments here...");

Methods

addClass(String... styleClasses)
JTextArea
Adds custom CSS style classes to the textarea.
textArea.addClass("custom-class", "large-text");
createWithLabel(String labelText, boolean required)
VBox
Creates a VBox container with a label and this textarea. If required is true, adds a red asterisk (*) next to the label.
VBox formField = textArea.createWithLabel("Description", true);
container.getChildren().add(formField);
setStatus(String styleClass)
JTextArea
Sets the validation status of the textarea by applying a status style class.
// Show success state
textArea.setStatus("form-input-success");

// Show error state
textArea.setStatus("form-input-danger");

// Clear status
textArea.setStatus(null);

Default Configuration

wrapText
boolean
default:"true"
Text wrapping is enabled by default
prefRowCount
int
default:"3"
Default height is set to 3 rows

Style Classes

j-text-area
CSS class
Custom style class applied to JTextArea instances
form-input
CSS class
Base style class automatically applied to all JTextArea instances (reuses common input styles)
form-label
CSS class
Style class applied to labels created with createWithLabel()
required-mark
CSS class
Style class applied to the asterisk (*) required indicator

Usage Examples

Basic Comment Form

import com.jjarroyo.components.JTextArea;
import javafx.scene.layout.VBox;

public class CommentForm {
    public VBox createForm() {
        VBox form = new VBox(10);
        
        // Basic textarea with label
        JTextArea commentArea = new JTextArea("Write your comment...");
        VBox commentField = commentArea.createWithLabel("Comment", true);
        
        form.getChildren().add(commentField);
        return form;
    }
}

Description Field with Validation

import com.jjarroyo.components.JTextArea;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

public class ProductForm {
    public VBox createDescriptionField() {
        VBox container = new VBox(5);
        
        JTextArea descriptionArea = new JTextArea("Enter product description...");
        VBox descriptionField = descriptionArea.createWithLabel("Description", true);
        
        Label charCount = new Label("0 / 500 characters");
        charCount.setStyle("-fx-text-fill: gray; -fx-font-size: 12px;");
        
        // Character count and validation
        descriptionArea.textProperty().addListener((obs, old, newVal) -> {
            int length = newVal.length();
            charCount.setText(length + " / 500 characters");
            
            if (length > 500) {
                descriptionArea.setStatus("form-input-danger");
                charCount.setStyle("-fx-text-fill: red; -fx-font-size: 12px;");
            } else if (length > 400) {
                descriptionArea.setStatus("form-input-warning");
                charCount.setStyle("-fx-text-fill: orange; -fx-font-size: 12px;");
            } else if (length >= 50) {
                descriptionArea.setStatus("form-input-success");
                charCount.setStyle("-fx-text-fill: green; -fx-font-size: 12px;");
            } else {
                descriptionArea.setStatus(null);
                charCount.setStyle("-fx-text-fill: gray; -fx-font-size: 12px;");
            }
        });
        
        container.getChildren().addAll(descriptionField, charCount);
        return container;
    }
}

Multi-line Address Input

import com.jjarroyo.components.JTextArea;
import javafx.scene.layout.VBox;

public class AddressForm {
    public VBox createAddressField() {
        VBox form = new VBox(10);
        
        JTextArea addressArea = new JTextArea("Street address, city, state, ZIP");
        addressArea.setPrefRowCount(4); // Taller for address
        
        VBox addressField = addressArea.createWithLabel("Shipping Address", true);
        
        form.getChildren().add(addressField);
        return form;
    }
}

Message Input with Custom Styling

import com.jjarroyo.components.JTextArea;
import javafx.scene.layout.VBox;

public class MessageForm {
    public VBox createMessageField() {
        VBox form = new VBox(10);
        
        JTextArea messageArea = new JTextArea("Type your message...")
            .addClass("large-text", "message-input");
        
        messageArea.setPrefRowCount(5);
        messageArea.setWrapText(true);
        
        VBox messageField = messageArea.createWithLabel("Message", true);
        
        form.getChildren().add(messageField);
        return form;
    }
}

Feedback Form with Validation

import com.jjarroyo.components.JTextArea;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;

public class FeedbackForm {
    public VBox createFeedbackForm() {
        VBox form = new VBox(15);
        
        JTextArea feedbackArea = new JTextArea("Tell us what you think...");
        feedbackArea.setPrefRowCount(6);
        
        VBox feedbackField = feedbackArea.createWithLabel("Feedback", true);
        
        Button submitBtn = new Button("Submit Feedback");
        submitBtn.setOnAction(e -> {
            String feedback = feedbackArea.getText().trim();
            
            if (feedback.isEmpty()) {
                feedbackArea.setStatus("form-input-danger");
            } else if (feedback.length() < 10) {
                feedbackArea.setStatus("form-input-warning");
            } else {
                feedbackArea.setStatus("form-input-success");
                submitFeedback(feedback);
            }
        });
        
        form.getChildren().addAll(feedbackField, submitBtn);
        return form;
    }
    
    private void submitFeedback(String feedback) {
        System.out.println("Feedback submitted: " + feedback);
    }
}

Inherited Methods

Since JTextArea extends TextArea, all standard JavaFX TextArea methods are available:
  • setText(String text) - Set the text content
  • getText() - Get the current text
  • setPromptText(String value) - Set placeholder text
  • setPrefRowCount(int rows) - Set preferred number of rows
  • setWrapText(boolean wrap) - Enable/disable text wrapping
  • setEditable(boolean value) - Enable/disable editing
  • setDisable(boolean value) - Enable/disable the textarea
  • textProperty() - Access the text property for binding
  • setPrefWidth(double width) - Set preferred width
  • setPrefHeight(double height) - Set preferred height
  • And all other TextArea methods

Common Patterns

Required Field Validation

JTextArea textArea = new JTextArea("Required field");
textArea.focusedProperty().addListener((obs, wasFocused, isFocused) -> {
    if (!isFocused && textArea.getText().trim().isEmpty()) {
        textArea.setStatus("form-input-danger");
    } else if (!textArea.getText().trim().isEmpty()) {
        textArea.setStatus("form-input-success");
    }
});

Character Limit Enforcement

JTextArea textArea = new JTextArea();
final int MAX_LENGTH = 500;

textArea.textProperty().addListener((obs, old, newVal) -> {
    if (newVal.length() > MAX_LENGTH) {
        textArea.setText(old);
    }
});

Auto-resize

JTextArea textArea = new JTextArea();
textArea.setWrapText(true);
textArea.setPrefRowCount(3);

// Auto-grow based on content
textArea.textProperty().addListener((obs, old, newVal) -> {
    int lines = newVal.split("\n").length;
    textArea.setPrefRowCount(Math.max(3, Math.min(10, lines)));
});

See Also

Build docs developers (and LLMs) love