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

Text Fields in JavaFX

Code Description:

The code provided below shows how to create a Text Field in JavaFX. Text Fields are boxes where a user can enter free-form text. Text Fields are limited to a single line unlike Text Areas.

Text Field Syntax:

TextField textField = new TextField();

Runnable JavaFX Text Field Example Code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
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();
		
	TextField textField = new TextField();
	textField.setPromptText("Type stuff here...");
					
	root.getChildren().addAll(textField);
		
	primaryStage.setTitle("Text Field 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 Field Output Image

Important Methods:

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

Related Links:

JavaFX Text Areas JavaFX Labels