Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Input verification for Host name (IP)

 
John Chien
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can't really help you on why it is not working on 1.4.
However, I will say this, why go through all that trouble when it won't get you any extra points, and in fact might make you lose points because it is beyond the requirements.
You don't need a verifier or a database of DNS names for this assignment.
Mark
 
John Chien
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark:
Thank you for the response.
I just change the design. Instead of using
DNSTextFiled, I use JTextField.
The input verification is done at the time button id pressed. It seeminly works fine.
Thanks,
John Chien
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic