Search code examples
javaswingcontrolsjtextfield

JTextField contain control method


I have a text field (for name )that i want to prevent user to enter number, Or empty!

I try all of this, But there is some infirmity in it:

public boolean nameControl(String str) {
    if (haveDigit(str)) return false;        // works corrctly
    else if (str.isEmpty()) return false;     // Not work
    else if (str.length() == 0) return false;    // not work
    return true;
}

Solution

  • As described in How to Use the Focus Subsystem: Validating Input, you may be looking for InputVerifier. There's a related example here, among others.