JTextField Error Checking Classes

advertisement
Using javax.swing.InputVerifier for error checking JTextfields and combo boxes
Below is code that will check to see if a JTextfield is empty when values are required. You must
1.import in javax.swing.InputVerifier; as well as the Swing classes.
2. Then declare an object of the StrictInputVerifierComboBox class at the top of the program and
instantiate it in the constructor.
3. Then in your listener class, when you are getting data from a Jtextfield or combo box, just call the
verify method to make sure it has a value in it.
Details
StrictInputVerifierComboBox tester; // declared at the top of the program
Instantiate the tester object in the constructor
boolean isTrue = false; // declared at top of listener class
In the listener class:
if(status.equalsIgnoreCase("R")) // If its not a senior client...
{
name = firstField.getText();
isTrue = tester.verify(firstField);
if(isTrue)
{
lastname = lastField.getText();
isTrue = tester.verify(lastField);
}
if(isTrue)
{
phone = phoneField.getText();
isTrue = tester.verify(phoneField);
}
if(isTrue)
{
client = new Client(name, lastname ,phone, sex.charAt(0),hobby, age);
List.add(client); // Add client
listModel.addElement(client); // If successful, add it to listbox
}
The dialog box will come up and tell the user that the field is empty for the each empty box and not
continue..
(Of course you will have to include testing all the fields.)
The advantage of this is a specialized dialog box informing the user that the field is empty and that you
can carry the verifier classes with you whenever you use textfields or combo boxes.
Now suppose you want to determine if the word “pass” has been put in the field, you could write a class
like this:
class PassVerifier extends InputVerifier
{
public boolean verify(JComponent input)
{
JTextField tf = (JTextField) input;
return "pass".equals(tf.getText());
}
}
Below is a link showing how to do this.
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/InputVerifier.html
The trouble with this example is there is no feedback to the user . You would have to figure out your
own. The example below gives feedback and you do have to throw an exception.
// Sample classes
import javax.swing.InputVerifier;
public class StrictInputVerifierComboBox extends InputVerifier {
public StrictInputVerifierComboBox() {
}
public boolean verify(JComponent input) {
JComboBox textField = (JComboBox)input;
if (textField.getSelectedIndex() != -1) {
return true;
} else {
JOptionPane.showMessageDialog(input, "A String value is required!", "Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
/
public class StrictInputVerifierTextField extends InputVerifier {
public StrictInputVerifierTextField() {
}
public boolean verify(JComponent input) {
JTextField textField = (JTextField)input;
if (!textField.getText().trim().equals("")) {
return true;
} else {
JOptionPane.showMessageDialog(input, "A String value is required!", "Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
Link to InputVerifier class.
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/class-use/InputVerifier.html
Download