JTextField là component quan trọng cho phép nhập văn bản một dòng. Việc giới hạn số ký tự user nhập vào JTextField đạt được bằng lớp PlainDocument.
Trong ví dụ dưới, triển khai logic bằng PlainDocument, cho phép user nhập tối đa 10 ký tự, không cho nhập quá 10.
Ví dụ
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
public class JTextFieldLimitTest extends JFrame {
JTextField textfield;
JLabel label;
public static void main(String[] args) {
new JTextFieldLimitTest().GUI();
}
public void GUI() {
setLayout(new FlowLayout());
label = new JLabel("max 10 chars");
textfield = new JTextField(15);
add(label);
add(textfield);
textfield.setDocument(new JTextFieldLimit(10));
setSize(350, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
Kết quả
