File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes want to set limitation column at textfield/textarea(Urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "want to set limitation column at textfield/textarea(Urgent)" Watch "want to set limitation column at textfield/textarea(Urgent)" New topic
Author

want to set limitation column at textfield/textarea(Urgent)

Samuel Chan
Greenhorn

Joined: Jun 10, 2001
Posts: 20
I want to limit the user only can enter 10 char into a textfield, but although i set 10 columns for the textfield, the user still can enter as many as they can.
Anybody can help?
thanks.
Laurent Leonard
Ranch Hand

Joined: May 15, 2001
Posts: 35
Try something like this.
JTextField tf = new JTextField();
PlainDocument pd = new PlainDocument() {
public void
insertString(int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str.length() + getLength() <= MAX_LENGTH )
{
super.insertString(offset, str, attr);
}
}
};
tf.setDocument(pd);

------------------
Laurent Leonard
Laurent.Leonard@belgique.com
[This message has been edited by Laurent Leonard (edited June 20, 2001).]


Laurent LEONARD
Jagan Mohan Reddy
Greenhorn

Joined: May 15, 2001
Posts: 18
Hi,
Your approach doesnot solve the problem.Add th key listener to the text field.
your keylistener looks something like this.
public void keyPressed(KeyEvent e){

int length = tf.getText().length();
if(length>=10)
tf.setText(tf.getText().substring(0,length-1));

}

Hope this solves ur problem.
Regards,
Jagan Mohan Reddy.
Martin webster
Greenhorn

Joined: Jun 20, 2001
Posts: 20
Hello,
The key listener apprach I.e
public void keyPressed(KeyEvent e){
int length = tf.getText().length();
if(length>=10)
tf.setText(tf.getText().substring(0,length-1));
}
would not work, as it always assumes that the carot position is at the end of the field... and it will not cope with pasting text into the field.(i.e multiple characters). Extending the document is the only solution I've found that can be used to restrict input into a text component.


Martin
Jagan Mohan Reddy
Greenhorn

Joined: May 15, 2001
Posts: 18
Hi,

Thanks for your answer.
Regards,
Jagan Mohan Reddy.
Samuel Chan
Greenhorn

Joined: Jun 10, 2001
Posts: 20
Hi,
Sorry,
what do u mean by "extending the document"??
That means what should I do?
Martin webster
Greenhorn

Joined: Jun 20, 2001
Posts: 20
Hi,
Code like this...
PlainDocument pd = new PlainDocument() {
public void
insertString(int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str.length() + getLength() <= MAX_LENGTH )
{
super.insertString(offset, str, attr);
}
}
};
creates a subclass of plain document with the insertString method overridden. I'm sure there's a proper name for this kind of subclassing, probably anonymous subclassing or something. I would rather create a proper inner class, of the form
private class FixedLengthDocument extends PlainDocument
{
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException
{
if (str.length() + getLength() <= MAX_LENGTH )
{
super.insertString(offset, str, attr);
}
}
}
Or more than likely, I would place this in its own .java file.
Samuel Chan
Greenhorn

Joined: Jun 10, 2001
Posts: 20
Thanks for your help!!
Samuel Chan
Greenhorn

Joined: Jun 10, 2001
Posts: 20
Hi,
I'm sorry that i can't do the thing Martin webster
mentioned before since i'm using a textfield but not jtextfield
is there any suggestion??
thanks
PierreArnaud Galiana
Greenhorn

Joined: Jun 13, 2001
Posts: 15
would be a nice day to switch to swing !
if your really want to keep your java.awt.TextField, then I assume you'll have to play with listeners...
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Samuel,
Just my $.02. Look into the TextListener. It should do the trick for you because the listener will get called for any textual change which includes, cutting, pasting, keystrokes, etc.
Good Luck,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want to set limitation column at textfield/textarea(Urgent)