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 textbox validation 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 "textbox validation" Watch "textbox validation" New topic
Author

textbox validation

Jeff Ciaccio
Greenhorn

Joined: Nov 26, 2008
Posts: 25
Hi all,

I've got a text box that expects a double between 0.1 and 0.99. I would like to validate this right when the user enters the number and then tries to change the focus to another component. Is there data validation built in to Java? NetBeans?

Thanks,
Jeff


Jeff Ciaccio, Java novice <br />Physics and AP Physics Teacher<br />Sprayberry High School <br />Marietta, GA
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

What kind of text box is it? Swing? HTML?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jeff Ciaccio
Greenhorn

Joined: Nov 26, 2008
Posts: 25
swing.
JTextField
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

One option is use a JFormattedTextField instead. Another is use an InputVerifier.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Sounds like something which would sit better on the Swing forum. Moving.
Jeff Ciaccio
Greenhorn

Joined: Nov 26, 2008
Posts: 25
I'm very green with Java, so I just wanted to do something simple like:
1) cast the text as double (but I'm not sure how)
2) do an if then to check:
if (txt > double is < 0.1 or > .99) then set focus back on textBox and change the label's font color to red and bold
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

Check this out to get an idea of how it's usually done.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
You can't "cast" text to a double. You can however use a Double class method whihc parses the String as a double.
 
jQuery in Action, 2nd edition
 
subject: textbox validation
 
Similar Threads
xml validation with the java 5
java.io.Serializable;
Validation interceptor
problem with Date validations thru Validator-rules.xml and Validation.xml
validating forms