• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Samuel Chan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Jagan Mohan Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jagan Mohan Reddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your answer.
Regards,
Jagan Mohan Reddy.
 
Samuel Chan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Sorry,
what do u mean by "extending the document"??
That means what should I do?
 
Martin webster
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help!!
 
Samuel Chan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic