Hi,
I try to implement a online database swap by creating a connection dialog that allows user to input the host DNS and port number.
For the Host DNS, I created a class called DNSTextField. This class can do the input verification. The code is shown below:
public class DNSTextField extends JTextField {
private DNSVerifier verifier = null;
public DNSTextField(
String label) {
super(label);
verifier = new DNSVerifier();
setInputVerifier(verifier);
}
public DNSVerifier getVerifier() {
return verifier;
}
public boolean verify() {
return verifier.verify(this);
}
private class DNSVerifier extends InputVerifier {
public boolean verify(JComponent source) {
JTextField textField = (JTextField) source;
boolean valid = true;
try {
String text = textField.getText();
if ((text == null) || (text.equals(""))) {
valid = false;
} else {
InetAddress ipAddr = InetAddress.getByName(text.trim());
}
}
catch (Throwable t) {
valid = false;
}
if (! valid) {
JOptionPane.showMessageDialog(textField,
"The value in the text field is invalid."
+ "\nPlease correct it.\n", "Invalid Input",
JOptionPane.ERROR_MESSAGE);
}
return valid;
}
}
}
If I run this code under JDK 1.3 with wrong host DNS input (something like "wer"), the GUI will show me an error dialog. However, I can not run it with JDK 1.4. The error dialog won't show. Besides that, it does not allow me transfer to another input field. Sometimes, it even get into some loop and throw some exception.
Does anybody know how to fix problem like this ? How can I have good input verification ? I would like to have one of the two thing done:
1) Show error dialog when the input is wrong.
2) Or --> be able to jump to other input field disregarding the error input.
Thanks,
John Chien