Hướng dẫn và ví dụ JavaFX TextField
Xem thêm các chuyên mục:

Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.


Class TextField thực hiện một điều khiển giao diện người dùng (UI Control) chấp nhận và hiển thị đầu vào văn bản. Nó cung cấp khả năng tiếp nhận đầu vào văn bản từ một người sử dụng. Một class tương tự khác là PasswordField cho phép người dùng nhập mật khẩu, lớp này mở rộng lớp TextInput

Xem một vài phương thức hữu ích bạn có thể sử dụng với TextField.
- clear() - Xóa bỏ văn bản trên TextField.
- copy() - Copy đoạn text đang được bôi đen trên TextField vào Clipboard.
- cut() - Cắt một đoạn text đang được bôi đen trên TextField và lưu vào Clipboard, đồng thời di chuyển vị trí con trỏ.
- paste() - Dán nội dung text trên Clipboard vào TextField tại vị trí con trỏ, thay thế đoạn text đang được bôi đen nếu có.

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);
}
}
Ví dụ dưới đây minh họa sử dụng các phương thức clear(), copy(), paste(), cut(), chúng là các phương thức hữu ích của 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");
// Nhấn vào nút này không làm mất tiêu điểm (Focus) của thành phần khác.
buttonCopy.setFocusTraversable(false);
buttonCopy.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
textField.copy();
}
});
// Cut
Button buttonCut = new Button("Cut");
// Nhấn vào nút này không làm mất tiêu điểm (Focus) của thành phần khác.
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);
}
}