mydomain
No ADS
No ADS

JavaFX TextField Tutorial with Examples

  1. JavaFX TextField
  2. TextField example
  3. Useful methods

1. JavaFX TextField

The TextField class implements a UI control that accepts and displays text input. It provides capabilities to receive text input from a user. Along with another text input control, PasswordField, this class extends the TextInput class.
Review some helpful methods that you can use with text fields.
  1. clear() - Clear the text of TextField.
  2. copy() - transfers the currently selected range in the text to the clipboard, leaving the current selection.
  3. cut() - transfers the currently selected range in the text to the clipboard, removing the current selection.
  4. paste() - transfers the contents in the clipboard into this text, replacing the current selection.

2. TextField example

No ADS
TextFieldDemo.java
package org.o7planning.javafx.textfield;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class TextFieldDemo  extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField textField = new TextField("Tran");
        textField.setMinWidth(120);

        FlowPane root = new FlowPane();
        root.setPadding(new Insets(10));

        root.getChildren().add(textField);

        Scene scene = new Scene(root, 200, 100);

        primaryStage.setTitle("JavaFX TextField (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

3. Useful methods

The example below illustrates the usage of methods such as clear(), copy(), paste(), cut(), which are usefull methods of TextField.
TextFieldDemo2.java
package org.o7planning.javafx.textfield;

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.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class TextFieldDemo2 extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        TextField textField = new TextField("This is a Text");
        textField.setMinWidth(180);

        // Clear
        Button buttonClear = new Button("Clear");
        buttonClear.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textField.clear();
            }
        });

        // Copy
        Button buttonCopy = new Button("Copy");

        // Click this button without losing focus of the other component
        buttonCopy.setFocusTraversable(false);

        buttonCopy.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textField.copy();
            }
        });

        // Cut
        Button buttonCut = new Button("Cut");

        // Click this button without losing focus of the other component
        buttonCut.setFocusTraversable(false);

        buttonCut.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textField.cut();
            }
        });

        // Paste
        Button buttonPaste = new Button("Paste");
        buttonPaste.setFocusTraversable(false);
        buttonPaste.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                textField.paste();
            }
        });

        FlowPane root = new FlowPane();
        root.setPadding(new Insets(10));
        root.setVgap(5);
        root.setHgap(5);

        root.getChildren().addAll(textField, buttonClear,
                buttonCopy, buttonCut, buttonPaste);

        Scene scene = new Scene(root, 200, 100);

        primaryStage.setTitle("JavaFX TextField (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}
No ADS

JavaFX Tutorials

Show More
No ADS