• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to limit text in AWT Text field ?

 
Lakshmi Geetha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using AWT text field (Jdk 1.1.7), I need to limit the
number of chars entered in the text field. For that i am
setting cloumn using setColumn and in the keyPressed method
of its KeyListener I am using the following code :
if (getColumn()!= -1)
{
char keyc = e.getKeyChar();
if (keyc >= 32 && keyc <= 126)<br /> {<br /> if ( (getCaretPosition() >= _len | |
getText().length() >= _len ) &&
getSelectionStart() == getSelectionEnd() )
{
e.consume();
}
}
This works fine in the normal case. But if I use Ctrl-C and Ctrl-V to cut-paste a string from somewhere , it takes any length of strings.
What is the possible solution to limit the length even in ctrl-c and ctrl-v ?

Thanks
--l
 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can check the link
http://www.javaranch.com/ubb/Forum2/HTML/001062.html

 
Javaoops
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi geeth,
I hope this link will solve your problem
http://www.cs.ucc.ie/systems/javatutorial/uiswing/components/generaltext.html
Cut /Copy & Paste of a string has been checked.
Check the example TextComponentDemo.java and LimitedStyledDocument.java in the link.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!