wood burning stoves
The moose likes Swing / AWT / SWT and the fly likes How to restrict data in JTextField? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to restrict data in JTextField?" Watch "How to restrict data in JTextField?" New topic

How to restrict data in JTextField?

Bob Zoloman
Ranch Hand

Joined: Jul 02, 2006
Posts: 72
I'm trying to make a JTextField that only accepts 0 or 1, as the input. Is there any way to do this? Thanks.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
use a DocumentFilter, or, if you mean the textfield will only be 1 character (0 or 1),
perhaps a JSpinner might suit
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Or a JCombobox, perhaps?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
I suppose the only other way would be to put validation logic in the method where you use the value in the textField?


[ November 06, 2006: Message edited by: John Bartlett ]
I agree. Here's the link: http://aspose.com/file-tools
subject: How to restrict data in JTextField?
It's not a secret anymore!