Re: Limit the number of characters that can be entered into JTextArea
CBO wrote:
Hi all,
I am currently developing a java application that has two JTextAreas.
I want to be able to limit the number of characters that a user can
enter to 50. How would I go about doing this.
Any help in this matter would be highly appreciated.
Thank you
//
//
// FixedLengthDocument
//
//
package com.knutejohnson.classes;
import javax.swing.text.*;
public class FixedLengthDocument extends PlainDocument {
private int length;
public FixedLengthDocument(int length) {
if (length <= 0)
throw new IllegalArgumentException("length <= 0");
this.length = length;
}
public void setDocumentLength(int length) {
if (length <= 0)
throw new IllegalArgumentException("length <= 0");
this.length = length;
}
public int getDocumentLength() {
return length;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str != null) {
if (str.length() + getLength() > length)
str = str.substring(0,length - getLength());
super.insertString(offs, str, a);
}
}
}
--
Knute Johnson
email s/nospam/knute2008/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access