Java Reference
In-Depth Information
Verifying Input During Focus Traversal
Swing offers the abstract InputVerifier class for component-level verification during focus
traversal with any JComponent . Just subclass InputVerifier and provide your own public
boolean verify(JComponent) method to verify the contents of the component.
Listing 2-14 provides a simple numeric text field verification example, showing three text
fields, of which only two have verification. Unless fields one and three are valid, you can't tab
out of them.
Listing 2-14. Numeric Input Verifier
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class VerifierSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Verifier Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
JTextField textField3 = new JTextField();
InputVerifier verifier = new InputVerifier() {
public boolean verify(JComponent comp) {
boolean returnValue;
JTextField textField = (JTextField)comp;
try {
Integer.parseInt(textField.getText());
returnValue = true;
} catch (NumberFormatException e) {
returnValue = false;
}
return returnValue;
}
};
textField1.setInputVerifier(verifier);
textField3.setInputVerifier(verifier);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.CENTER);
frame.add(textField3, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
Search WWH ::




Custom Search