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
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply 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: 32833
    
    4
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: 8566

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


[Donate a pint, save a life!] [How to ask questions] [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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.awt.TextField - limiting input size?
 
Similar Threads
input displays just the first word
setting text alignment in a TextField
How to set maxlength for java.awt.TextField
trying to fix an unreported exception
Netscape - Input Boxes