LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-09-2004, 01:09 PM   #1
Andy@DP
Member
 
Registered: Aug 2003
Posts: 208

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.

TIA,
Andy.
 
Old 03-09-2004, 02:23 PM   #2
Looking_Lost
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, 03:46 PM   #3
Andy@DP
Member
 
Registered: Aug 2003
Posts: 208

Original Poster
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, 01:16 AM   #4
german
Member
 
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));

Code:
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) 
      return;

    super.insertString(offs, str, a);
  }
}
HTH

B.
 
  


Reply



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 08:25 PM
java swing JTextArea gauravbagga Programming 1 05-23-2005 04:34 AM
Limiting the amount of bandwidth Apache Uses wenberg Linux - Software 0 02-07-2005 11:39 AM
PHP: limiting amount of text printed jme Programming 1 11-13-2004 08:13 AM
Limiting the total amount of memory usage by apache. SplashHost.com Linux - General 8 08-30-2002 09:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 AM.

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