|
|
Let’s consider a common coding situation. Suppose you have a text field in your form that accepts an email address. Suppose you need to code such that if the email address typed by the user is not valid, then you shouldn’t let the focus out of this textfield. Where would you put such a code? In the focus lost of the email-text-field? In the focus gained for all other text-fields? I don’t know what’s the best way to do this in JavaScript but there’s a super-elegant way in Swing! The need for InputVerifierTo understand that let’s see the problems with the other common approach to solve this problem, using the focusLost() of the textfield The problem with Focus-Snatching: One of the common approaches people take to solve such validation issue is to put the code in the focuslost() of a FocusListener attached to the textfield, and if the validation fails the requesting the focus back to the source component. This approach has several problems, but the main is this: When two things are wrong, does it matter which was wrong first?
while there are ways to code to stop this from occuring, including putting this code in the focusgained(), of other components,and having flags to prevent this, why not use a better way provided in swing! Enter InputVerifier: It’s great if you could fight and claim back the focus that belongs to you after letting it go to someone else, but wouldn’t it be better if you did not let it go in the first place?
Please ..Anything but papayasEver tasted a papaya? Some poeple like it. Some of my friends love it. It’s the fruit that I hate most! It’s so… soo.. yucky! So, as a dedication to my hatred, Let’s make a textfield accept anything, but the word “papaya” The AnitPapayaVerifier: Here’s the code for the AnitPapayaVerifier class. See comments in the code for understanding //class that starts the the anti-papaya movement class AntiPapayaVerifier extends InputVerifier { //Override the verify method to write the logic and then //return true or false @Override public boolean verify(JComponent input) { //Get the text in the text field JTextField couldBeEvilField = (JTextField)input; String text = couldBeEvilField.getText().toLowerCase(); //Check if the user has typed the p-word if(text.indexOf("papaya")!= -1){ //show some arrogant message, and //then return false //Bad guyz eat papya JOptionPane.showMessageDialog(null, "my JForm " + "dosen't grow papayas"); //returning false ensures the focus, is not //moved out of the attached textfield return false; } else{ //Nice guyz don't eat papaya //return true to ensure that the //the focus is moved to where the //user wishes to return true; } } }
Now all we need to do is attach it our textfield JTextField noPapayaField = new JTextField(""); noPapayaField.setInputVerifier(new AntiPapayaVerifier()); That’s it. Now your evil user dosen’t stand a chance against typing papaya in your text field and getting away with that. 3 Responses to 'Swing’s InputVerifier tutorial: A super elegant way for validation'Leave a Reply |
Popular Articles
Blog Categories
Monthly Archives
Resources
|
[…] has a short tutorial on InputVerifier, including background on relevant scenarios and sample […]
Don’t forget to check the really great JGoodies Validation library :
http://www.jgoodies.com/downloads/libraries.html
[…] components in whatever way you like, looking at the code can give you an excellent insight on using InputVerifier and DocumentFilter if you have already not used them Posted in basic, components, […]