This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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


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: 43905
    
  32
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: 10744
    
    9

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?
 
It's not a secret anymore!