This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 38048
    
  22
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: 10172
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.awt.TextField - limiting input size?
 
Similar Threads
Netscape - Input Boxes
trying to fix an unreported exception
setting text alignment in a TextField
input displays just the first word
How to set maxlength for java.awt.TextField