1) devi definire un nuovo tipo di Document il cui metodo "insertString" deve essere + o - così (preso pari pari dai tutorial della SUN: perchè non li guardate mai????)
2) definisci un nuovo tipo di JTextField il cui metodo "createDefaultModel" ritorna una istanza del Document che hai definito in precedenza.codice:public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { //This rejects the entire insertion if it would make //the contents too long. Another option would be //to truncate the inserted string so the contents //would be exactly maxCharacters in length. if ((getLength() + str.length()) <= maxCharacters) super.insertString(offs, str, a); else Toolkit.getDefaultToolkit().beep(); }
3) il gioco è fatto.

Rispondi quotando