Validating Strings in JTextFields

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Hi Folks,

I've created a class which allows me to validate strings being entered into a JTextField, I've extended javax.swing.text.PlainDocument. And now I can create a JTextField like so

Code:
JTextField tf = new JTextField(new ValidatorDocument(_validatorString, _fieldLength),"",15);

This basically restricts what characters and how many of them can be entered into the text field. I.e. the validatorString only contains digits, if the user tries to enter any other character it won't let them.

Now I'd like to try and extend this functionality to allow users to enter a time. So my _validatorString currently reads "0123456789:". Of course this doesn't stop the user from entering a stupid time like 25:00,:2500,25:79 etc. etc.

Any ideas how I can restrict the user to only entering in the format

00-23 : 00-59

Thanks in advance,
Paul
 
Do you have the code for the implementation of the ValidatorDocument class? It will help us out a lot as we can see what's hapenning and where the changes would need to be made.
 
Yeah sure, here's the relevant bits

Code:
import javax.swing.text.PlainDocument;

public class ValidatorDocument extends PlainDocument {

   public static final String NUMERIC = "1234567890";

  public ValidatorDocument() {
    super();
  }

  public ValidatorDocument(String chars, int length) {
    super();

    setMaxLength(length);
    setValidChars(chars);
  }
  
  public ValidatorDocument(String chars, int length, boolean convertToUpper) {
    this(chars,length);
    _convertToUpper = convertToUpper;
  }

  public void setMaxLength(int maxLength) {
    _maxLength = maxLength;
    if (_maxLength <= 0) {
      _maxLength = 255;
    }
  }

  public int getMaxLength() {
    return _maxLength;
  }

  public void setValidChars(String validChars) {
    _validChars = validChars;
  }

  public String getValidChars() {
    return _validChars;
  }

  public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {
    if (str == null) {
      return;
    }
    char[] allChar = str.toCharArray();
    if (_convertToUpper) {
      allChar = str.toUpperCase().toCharArray();
    }
    StringBuffer validString = new StringBuffer();

    for (int i = 0; i < allChar.length; i++) {
      if (_validChars == null || ((_validChars.indexOf(allChar[i]) != -1) && (offs < _maxLength)) ){
        validString.append(allChar[i]);
      }
    }
    if ((validString.length() + getLength()) > _maxLength) {
      String s = validString.substring(0, _maxLength - getLength());
      super.insertString(offs, s, a);
    } else {
      super.insertString(offs, validString.toString(), a);
    }
  }

  public boolean isValid(String value) {
    boolean valid = true;
    if (value != null) {
      // check length first
      valid = value.length() <= _maxLength;
      if (valid && _validChars != null) {
        // check characters
        char[] allChar = value.toCharArray();
        if (_convertToUpper) {
          allChar = value.toUpperCase().toCharArray();
        }
        for (int i = 0; i < allChar.length; i++) {
          if ((_validChars.indexOf(allChar[i]) == -1)) {
            valid = false;
          }
        }
      }
    }
    return valid;
  }
  
  public void setConvertToUpper(boolean b) {
    _convertToUpper = b;
  }

  public boolean isConvertToUpper() {
    return _convertToUpper;
  }
}
 
Back
Top Bottom