Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-09-2004, 02:09 PM   #1
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208
Blog Entries: 2

Rep: Reputation: 30
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.

Old 03-09-2004, 03:23 PM   #2
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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

int maxChars=10;

JTextArea tArea=new JTextArea();

AbstractDocument pDoc=(AbstractDocument)tArea.getDocument();

pDoc.setDocumentFilter(new DocumentSizeFilter(maxChars));
Old 03-09-2004, 04:46 PM   #3
Registered: Aug 2003
Location: Aberdeen, UK.
Distribution: Debian, Ubuntu
Posts: 208

Original Poster
Blog Entries: 2

Rep: Reputation: 30
Thanks Looking_Lost,
That link has inspired me to a workable solution!

Good tae get a straight answer fae a fellow Scot!
Old 03-10-2004, 02:16 AM   #4
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
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));

static class CustomDocument extends PlainDocument {
  private static int MAX_LENGTH = 50;
  private JTextArea tField;

  UpperCaseDocument(JTextArea field) {
    tField = field;

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

    if(str == null | | tField.getText().length() > MAX_LENGTH) 

    super.insertString(offs, str, a);



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
JTextArea Question for Java Programmers Diederick Programming 1 09-16-2005 09:25 PM
java swing JTextArea gauravbagga Programming 1 05-23-2005 05:34 AM
Limiting the amount of bandwidth Apache Uses wenberg Linux - Software 0 02-07-2005 12:39 PM
PHP: limiting amount of text printed jme Programming 1 11-13-2004 09:13 AM
Limiting the total amount of memory usage by apache. Linux - General 8 08-30-2002 10:56 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration