Updated April 12, 2023
Definition of JavaFX TextArea
The textarea is an input element using for multiple line information in the web application. It is providing multiple lines of input information from the user and store in the application. It is interacted between user and application using a textarea object and set in the database. It is a multi-line editor to getting more than one line of data from the user through the form and message box in the application. It is the latest version does not allow single-line information in the textarea box.
Syntax:
- The JavaFX Textarea needed the Textarea class and their object.
- This object is connected with the scene graph using a holding layout container.
- The JavaFX Textarea syntax is below.
TextArea jfxTextarea = new TextArea();
VBox jfxbox = new VBox(jfxTextarea);
Scene jfxscene = new Scene(jfxbox);
- The JavaFX textarea sets the text syntax using an object.
jfxTextarea.setText("text_here..");
- The JavaFX Textarea returns the text syntax using an object.
String textObject = jfxTextarea.getText();
How JavaFX TextArea function works?
- Download and Install the JDK and java IDE like eclipse on your computer device.
- Either add the JavaFX jar files in the library or install e(fx) clipse software in java IDE with the latest version.
- The start method is added to setup the JavaFX Application with the Stage object.
public class MainClass extends Application{
@Override
public void start(Stage jfxStage) throws Exception{
//Add javaFX Textarea methods and constructor
}
}
- Create the Textarea object inside of the start method.
- The Vbox is a vertical layout container that is useful for textarea.
public void start(Stage jfxStage) throws Exception{
TextArea jfxTextarea = new TextArea();
VBox jfxbox = new VBox(jfxTextarea);
Scene jfxscene = new Scene(jfxbox);
}
- This object is connected with the scene graph using a holding layout container.
public void start(Stage jfxStage) throws Exception{
TextArea jfxTextarea = new TextArea();
VBox jfxbox = new VBox(jfxTextarea);
Scene jfxscene = new Scene(jfxbox, 320, 160);
jfxStage.setScene(jfx scene);
jfxStage.show(); }
- It is used start() and main method to launch the application.
public class MainClass extends Application{
@Override
public void start(Stage jfxStage) throws Exception{
TextArea jfxTextarea = new TextArea();
VBox jfxbox = new VBox(jfxTextarea);
Scene jfxscene = new Scene(jfxbox, 320, 160);
jfxStage.setScene(jfxscene);
jfxStage.show(); }
}
public static void main(String[] args) {
Application.launch(args);
}
}
Constructors
- The JavaFX used stage constructor to show the window in the desktop application.
- The Stage constructor is working as a container to hold the application.
- The theoretical constructor and its object creating syntax is below.
Stage jfxstage = new Stage();
- The JavaFX constructor and its object always passing through the start() method.
public void start(Stage jfxStage){ implementation code.. }
Methods
- The start() method: The start method is the initialized method in the JavaFX application. The start method contains and passes all desktop application objects, methods. It always comes with start() method with the Stage constructor.
public void start(Stage jfxStage)
{
Implementation code...
}
- The launch() method: The launch() method is necessary to create an application for desktop using JavaFX. Creates the main method in the class and uses the launch() method with command line arguments.
public static void main(String[] args) {
Application.launch(args);
}
Examples of JavaFX TextArea
Let us discuss examples of JavaFX TextArea.
Example #1
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage jfxStage) throws Exception {
TextArea jfxtextArea = new TextArea();
VBox jfxvbox = new VBox(jfxtextArea);
Scene jfxscene = new Scene(jfxvbox, 200, 100);
jfxStage.setScene(jfxscene);
jfxStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
Description:
- The textarea creates an object and connecting in the vertical box container.
- The Vbox is interacted with the Scene node to contain Javafx textarea.
Example #2: Label and Padding
Code:
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage jfxstage) throws Exception {
Label jfxlabel = new Label( "Write below" );
TextArea jfxarea = new TextArea();
jfxarea.setPrefColumnCount(15);
jfxarea.setPrefHeight(120);
jfxarea.setPrefWidth(300);
VBox jfxbox = new VBox();
jfxbox.setSpacing(20);
jfxbox.setPadding(new Insets(20, 50, 50, 60));
jfxbox.getChildren().addAll(jfxlabel, jfxarea);
Scene jfxscene = new Scene(jfxbox, 590, 230);
jfxstage.setTitle( "JavaFX TextArea" );
jfxstage.setScene(jfxscene);
jfxstage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
Description:
- The setPadding() and setSpacing() methods are used to create the required space between textarea and container.
- The setPrefHeight() and setPrefWidth() method create required height and width of textarea.
Example #3: Set and return information
Code:
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage jfxstage) throws Exception {
VBox jfxvbox = new VBox();
jfxvbox.setPadding(new Insets(10));
jfxvbox.setSpacing(5);
Label jfxlabel = new Label("Write Something");
TextArea jfxtextArea = new TextArea();
jfxtextArea.setText("write below.\n");
Button jfxbutton = new Button("Click Here");
jfxbutton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
jfxtextArea.setText("write JavaFx Textarea information.\n");
String jfxtext = jfxtextArea.getText();
jfxtextArea.appendText( jfxtext);
}
});
jfxvbox.getChildren().addAll(jfxlabel, jfxtextArea,jfxbutton);
Scene jfxscene = new Scene(jfxvbox, 360, 250);
jfxstage.setTitle("JavaFX TextArea window");
jfxstage.setScene(jfxscene);
jfxstage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
- Set Text Output:
- Return Text Output:
Example #4: Current Date
Code:
package application;
import java.util.Date;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage jfxstage) throws Exception {
VBox jfxvbox = new VBox();
jfxvbox.setPadding(new Insets(10));
jfxvbox.setSpacing(5);
Label jfxlabel = new Label("Enter Date here..");
TextArea jfxtextArea = new TextArea();
jfxtextArea.setText(" Current Date: \n ");
VBox jfxhbar = new VBox();
Button jfxbutton = new Button("Date");
jfxbutton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
jfxtextArea.appendText( new Date().toString());
jfxtextArea.appendText("\n");
}
});
jfxhbar.getChildren().addAll(jfxbutton);
jfxvbox.getChildren().addAll(jfxlabel, jfxtextArea, jfxhbar);
Scene jfxscene = new Scene(jfxvbox, 360, 250);
jfxstage.setTitle("JavaFX TextArea window");
jfxstage.setScene(jfxscene);
jfxstage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
Conclusion
It is mainly useful for feedback, messages, and address input in the application form. This is useful for an unlimited character to input information from the user.
Recommended Articles
This is a guide to JavaFX TextArea. Here we discuss the definition and How JavaFX TextArea Function Works? along with examples. You may also have a look at the following articles to learn more –