1

I want to filter user's keyboard input for illegal/forbidden filename characters in JTextField. I have already set uppercase filter in JTextField.

DocumentFilter dfilter = new UpcaseFilter();
JTextField codeTF = new JTextField();
((AbstractDocument) codeTF.getDocument()).setDocumentFilter(dfilter);

Here is filter that I use to change lowercase to uppercase in JTextfield.

class UpcaseFilter extends DocumentFilter
{
    public void insertString (DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
    {
        fb.insertString (offset, text.toUpperCase(), attr);
    }

    public void replace (DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
    {
        fb.replace(offset, length, text.toUpperCase(), attr);
    }
}

How to I solve this problem?

jzd
  • 23,473
  • 9
  • 54
  • 76
Mikko Kirkanen
  • 179
  • 4
  • 17

4 Answers4

4

Something along these lines:

class FileNameFilter extends DocumentFilter {
  public void insertString (DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
    fb.insertString (offset, fixText(text), attr);
}

  public void replace (DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException {
    fb.replace(offset, length, fixText(text), attr);
  }

  private String fixText(String s) {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < s.lenght(); ++i) {
      if(isLegalFileNameChar(s.charAt(i))
        sb.append(s.charAt(i));
    }

    return sb.toString();
  }

  private boolean isLegalFileNameChar(char c) { 
    // Your logic goes here ...
  }
}
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
3

Use JFormattedTextField - see here and here

Formatted text fields provide a way for developers to specify the valid set of characters that can be typed in a text field

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    Unfortunately JFormattedTextField does not work for this purpose because it does not prevent _entering_ illegal characters, but checks later. – mh. Apr 29 '11 at 17:18
2

Big thanks for answers. I decide to use Itay's answer to solve the problem. Here is my solution.

DocumentFilter dfilter = new FileNameFilter();
JTextField codeTF = new JTextField();
((AbstractDocument) codeTF.getDocument()).setDocumentFilter(dfilter);

Here is FileNameFilter which block inserted illegal characters. This should work in Unix, Windows and Mac OS.

class FileNameFilter extends DocumentFilter
{
    private static final char[] ILLEGAL_CHARACTERS = {'/', '\n', '\r', '\t', '\0', '\f', '`', '?', '*', '\\', '<', '>', '|', '\"', ':', '.'};

    public void insertString (DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
    {
        fb.insertString (offset, fixText(text).toUpperCase(), attr);
    }

    public void replace (DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
    {
        fb.replace(offset, length, fixText(text).toUpperCase(), attr);
    }

    private String fixText (String s)
    {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < s.length(); ++i)
        {
            if (!isIllegalFileNameChar (s.charAt (i)))
                sb.append (s.charAt (i));
        }
        return sb.toString();
    }

    private boolean isIllegalFileNameChar (char c)
    {
        boolean isIllegal = false;
        for (int i = 0; i < ILLEGAL_CHARACTERS.length; i++)
        {
            if (c == ILLEGAL_CHARACTERS[i])
                isIllegal = true;
        }
        return isIllegal;
    }
}

JFormattedTextField also seems to be a good solution, but Itay's answer was simpler for me. Thank you very much!

Mikko Kirkanen
  • 179
  • 4
  • 17
0

InputVerifier complements JFormattedTextField nicely, as seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045