• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.awt.TextField - limiting input size?

 
James Hodgkiss
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?
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic