FileDocCategorySizeDatePackage
MaxLengthDocument.javaAPI DocExample1343Thu Oct 24 20:14:26 BST 2002None

MaxLengthDocument

public class MaxLengthDocument extends PlainDocument

Fields Summary
private int
max
Constructors Summary
public MaxLengthDocument(int maxLength)

    max = maxLength;
  
Methods Summary
public voidinsertString(int offset, java.lang.String str, javax.swing.text.AttributeSet a)

    if (getLength() + str.length() > max)
         java.awt.Toolkit.getDefaultToolkit().beep();
    else super.insertString(offset, str, a);
  
public static voidmain(java.lang.String[] args)


    Document doc = new MaxLengthDocument(5); // set maximum length to 5
    JTextField field = new JTextField(doc, "", 8);

    JPanel flowPanel = new JPanel();
    flowPanel.add(field);
    JFrame frame = new JFrame("MaxLengthDocument demo");
    frame.setContentPane(flowPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(160, 80);
    frame.setVisible(true);