import javax.swing.*;import javax.swing.text.*;public class Feld extends JFrame{ JTextField tf = new JTextField(); public Feld() { getContentPane().add(tf); tf.setDocument(new JTextFieldLimit(4)); } public static void main(String[] args) { Feld f = new Feld(); f.setSize(300,50); f.setLocation(200,200); f.show(); } public class JTextFieldLimit extends PlainDocument { private int limit; // optional uppercase conversion private boolean toUppercase = false; JTextFieldLimit() { super(); } JTextFieldLimit(int limit) { super(); this.limit = limit; } JTextFieldLimit(int limit, boolean upper) { super(); this.limit = limit; toUppercase = upper; } public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) { return; } if ((getLength() + str.length()) <= limit) { if (toUppercase) { str = str.toUpperCase(); } super.insertString(offset, str, attr); } } }}