• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

java.awt.TextField - limiting input size?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone an AWT solution for how I can limit the input size of my java.awt.TextField so the user cannot enter more than 4 characters?
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just off the top of my head:-

Add some sort of Listener which throws an Exception if getText().length() > 4

???

There must be other things you can do.
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by James Hodgkiss:
Does anyone an AWT solution for how I can limit the input size of my java.awt.TextField so the user cannot enter more than 4 characters?



Take a look at PlainDocument#insertString
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for the replies.

Got it sorted by adding a KeyListener to my TextField and using a conditional e.consume() in my keyTyped(Event e) method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!