This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
want to set limitation column at textfield/textarea(Urgent)
Samuel Chan
Greenhorn
Joined: Jun 10, 2001
Posts: 20
posted
0
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 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
posted
0
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
posted
0
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
posted
0
Hi,
Thanks for your answer. Regards, Jagan Mohan Reddy.
Samuel Chan
Greenhorn
Joined: Jun 10, 2001
Posts: 20
posted
0
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
posted
0
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
posted
0
Thanks for your help!!
Samuel Chan
Greenhorn
Joined: Jun 10, 2001
Posts: 20
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: want to set limitation column at textfield/textarea(Urgent)