This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Validation required in multiple Textfields in java swing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Validation required in multiple Textfields in java swing" Watch "Validation required in multiple Textfields in java swing" New topic
Author

Validation required in multiple Textfields in java swing

Bharvi Dixit
Greenhorn

Joined: Feb 14, 2013
Posts: 20
I have developed a swing form in which multiple textfields are required to be validated like - Textfieds can contain maximum 10 digits in which only 2 digits after the decimal are allowed (Ex.- 4562.10) Please tell me how to so it by using KeyListner or any other way.. Please help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

KeyListener is the worst of all possible choices. The standard way to be notified when a Swing text field is changed is to add a suitable DocumentListener to it, but really you should start by asking yourself whether you want to validate the field while data is being entered into it (harder than you might think if you didn't think of all the ways a user could mess up), or whether you want to wait until the user tells you they are finished. And you should consider whether using a formatted text field would allow you to avoid a lot of ugly coding.
Bharvi Dixit
Greenhorn

Joined: Feb 14, 2013
Posts: 20
Thanks Paul for the reply, You are absolutely right as there is lot of ugly coding with KeyListener, especially when i am having so many fields in the form. But, I i am a newbie to the java, and having problem in getting familiar with the concept of these DocumentListener or jFormattedTextField. One thing for sure that i want to validate the field while data is being entered into it. Please tell me that what should i do. I am ready to learn more and more..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Before you start writing code to validate the data as it's keyed in, then, you should take some time to consider how you're going to implement that. Being a beginner, you might think that throwing up a dialog box when you find the field's contents are invalid is the right thing to do, but it isn't. And remember that the fields start out blank -- do you consider that valid?

As for how to use a DocumentListener, start with the documentation for JTextField. (<== Follow that link.) It shows you a basic example of how to use a DocumentListener.
 
 
subject: Validation required in multiple Textfields in java swing