TextArea maximum input characters

Hello, can you please tell me how to set a maximum number of characters that can be input to a JTextArea component? I can't find it in the standard library, only dimensional restrictions and wrap settings. Thanks!
[221 byte] By [jhoa] at [2007-10-2 20:56:26]
# 1
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/DocumentFilter.html
Michael_Dunna at 2007-7-13 23:41:00 > top of Java-index,Desktop,Core GUI APIs...
# 2
I sort of see how the DocumentFilter could be used to set the maximum input, but I can't seem to get it. Can you provide a code example using JTextAreas? Your time and help is appreciated!
jhoa at 2007-7-13 23:41:00 > top of Java-index,Desktop,Core GUI APIs...
# 3

here's a simple demo using PlainDocument (might suit your needs)

import java.awt.*;

import javax.swing.*;

import javax.swing.text.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(400,300);

setDefaultCloseOperation(EXIT_ON_CLOSE);

JTextArea ta = new JTextArea(new CharacterLimiter(20),"",5,10);

ta.setLineWrap(true);

JPanel p = new JPanel();

p.add(ta);

getContentPane().add(p);

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

class CharacterLimiter extends PlainDocument

{

int maxChar;

public CharacterLimiter(int len){maxChar = len;}

public void insertString(int offs, String str, AttributeSet a) throws BadLocationException

{

if (this.getLength() + str.length() > maxChar)

{

java.awt.Toolkit.getDefaultToolkit().beep();

return;

}

super.insertString(offs, str, a);

}

}

Michael_Dunna at 2007-7-13 23:41:00 > top of Java-index,Desktop,Core GUI APIs...
# 4
Perfect! Thank you very much for your prompt response!
jhoa at 2007-7-13 23:41:00 > top of Java-index,Desktop,Core GUI APIs...