wood burning stoves 2.0*
The moose likes Java in General and the fly likes Validating Strings in JTextFields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Validating Strings in JTextFields" Watch "Validating Strings in JTextFields" New topic
Author

Validating Strings in JTextFields

Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 40
Hi Folks,

I've created a class which allows me to validate strings being entered into a JTextField, I've extended javax.swing.text.PlainDocument. And now I can create a JTextField like so



This basically restricts what characters and how many of them can be entered into the text field. I.e. the validatorString only contains digits, if the user tries to enter any other character it won't let them.

Now I'd like to try and extend this functionality to allow users to enter a time. So my _validatorString currently reads "0123456789:". Of course this doesn't stop the user from entering a stupid time like 25:00,:2500,25:79 etc. etc.

Any ideas how I can restrict the user to only entering in the format

00-23 : 00-59

Thanks in advance,
Paul
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Paul,

A regex is probably what you're looking for.

John.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Just keep in mind that the user will not always have a complete string typed in. Perhaps you can auto-complete it for him:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: Validating Strings in JTextFields
 
Similar Threads
NX: maximum input length of JTextField
Error message
Errors in GUI program
Only allow Strings to be Scanned with scanner.
Running...tally...?