• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Restricting the TextField To accept only a set no of characters

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic