For this purpose you will need a DocumentFilter . As it is applied, it filters documents.
Something like...
public class SizeFilter extends DocumentFilter { private int maxCharacters; public SizeFilter(int maxChars) { maxCharacters = maxChars; } public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException { if ((fb.getDocument().getLength() + str.length()) <= maxCharacters) super.insertString(fb, offs, str, a); else Toolkit.getDefaultToolkit().beep(); } public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException { if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters) super.replace(fb, offs, length, str, a); else Toolkit.getDefaultToolkit().beep(); } }
Create in MDP Weblog
Madprogrammer
source share