Java JTextArea - limiting amount of characters entered
I have an app I'm working on that uses Swing GUI components and need to limit the character length of a JTextArea. I know I can override the keydown() method of the Component class (i think) to check length of inputted text before allowing a character to be written to screen but I wondered if there was an easier way to do this.
I have scoured the Java API and come up with nothing.
There's info on it on suns Java Trail site here
Implementing Document Filter
Looks like you get the Document model associated with the JTextArea ( PlainDocument) and cast to AbastractDocument and create/set your own DocumentFilter for the JTextArea
The code is there in the DocumentFilter link on that page. You could use the class example provided and implement it something like this for a JTextArea
JTextArea tArea=new JTextArea();
That link has inspired me to a workable solution! :)
Good tae get a straight answer fae a fellow Scot!
You can do lots of cool stuff with JTextArea's by creating custom Document classes like this (I haven't even compiled this code but it should work)... just do jTextField.setDocument(new CustomDocument(jTextField));
|All times are GMT -5. The time now is 08:40 PM.|