Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Restricting the TextField To accept only a set no of characters  RSS feed

 
Sriram Sankar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i am going to enter some text inside the TextField ,the TextField should restrict me typing more than a particular no of
characters like ,it should not allow me to enter more than 10
characters,If u r getting any code for that plz let me know
Regards
Srinivas
 
Raajesh Chandran
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi srinivas!!

What constructor are U using to define for that TextField.Let me know that particular part.
Regards,
chandran
 
John Zukowski
Author
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To limit the number of characters in an AWT TextField, you need to attach a keyboard listener and count.
------------------
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
 
burugupalli
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi srinivas
by using subString in keyRelased u can ristrict allowing charecters.u get the text from the textfield then use if condition if its more than substring the text
i think so
 
Rajesh Adukkadukkath
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent ke) {
Object source = ke.getSource();

System.out.println("Code of key pressed:"+ke.getKeyCode());
if(source == jTextField1 )
{

if(ke.getKeyCode() == 10)
{
System.out.println("Enter key pressed");

}
//like this by finding key codes corresponding to the key you
want to restrict and then by giving if conditions you can do this.
}
}
);

with regards,
Rajesh Adukkadukkath
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!