import javax.swing.*;
import javax.swing.text.*;public class JTextFieldTester
{
  public static void main(String[] args)
  {
    JFrame f = new JFrame();
    JTextField tf = new JTextField();
    tf.setDocument(new LengthDocument());
    f.getContentPane().add(tf);
    f.setBounds(100, 100, 200, 160);
    f.setVisible(true);
  }
}class LengthDocument extends PlainDocument
{
public void insertString(int offset, String s, AttributeSet attrSet)
throws BadLocationException
{
if (this.getLength() >= 10)
{
return;
}

super.insertString(offset, s, attrSet);
}
}