aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes java.awt.TextField - limiting input size? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "java.awt.TextField - limiting input size?" Watch "java.awt.TextField - limiting input size?" New topic
Author

java.awt.TextField - limiting input size?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
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

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
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.
 
Consider Paul's rocket mass heater.
 
subject: java.awt.TextField - limiting input size?