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
Creates a new JTextArea with no prompt text. JTextArea textArea = new JTextArea ();
JTextArea(String promptText)
Creates a new JTextArea with the specified prompt text. The placeholder text to display when the textarea is empty
JTextArea textArea = new JTextArea ( "Enter your comments here..." );
Methods
addClass(String... styleClasses)
Adds custom CSS style classes to the textarea. One or more CSS class names to add
Returns this instance for method chaining
textArea . addClass ( "custom-class" , "large-text" );
createWithLabel(String labelText, boolean required)
Creates a VBox container with a label and this textarea. If required is true, adds a red asterisk (*) next to the label. The text to display in the label
Whether to show a required indicator (*)
A VBox containing the label and textarea with 4px spacing
VBox formField = textArea . createWithLabel ( "Description" , true );
container . getChildren (). add (formField);
setStatus(String styleClass)
Sets the validation status of the textarea by applying a status style class. The validation style class to apply. Valid values:
"form-input-success" - Green border for valid input
"form-input-danger" - Red border for errors
"form-input-warning" - Orange border for warnings
"form-input-dark" - Dark border variant
null or empty string - Removes all validation styles
Returns this instance for method chaining
// Show success state
textArea . setStatus ( "form-input-success" );
// Show error state
textArea . setStatus ( "form-input-danger" );
// Clear status
textArea . setStatus ( null );
Default Configuration
Text wrapping is enabled by default
Default height is set to 3 rows
Style Classes
Custom style class applied to JTextArea instances
Base style class automatically applied to all JTextArea instances (reuses common input styles)
Style class applied to labels created with createWithLabel()
Style class applied to the asterisk (*) required indicator
Usage Examples
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