如何从 JTextField 中的用户键盘输入中过滤非法/禁止的文件名字符?

发布于 2024-11-03 21:16:32 字数 809 浏览 1 评论 0原文

我想过滤用户键盘输入中 JTextField 中的非法/禁止文件名字符。我已经在 J​​TextField 中设置了大写过滤器。

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

这是我用来将 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);
    }
}

我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨死的猪 2024-11-10 21:16:32

沿着这些思路:

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 ...
  }
}

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 ...
  }
}
狼亦尘 2024-11-10 21:16:32

使用 JFormattedTextField - 参见此处此处

格式化文本字段为开发人员提供了一种指定可在文本字段中输入的有效字符集的方法

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

不疑不惑不回忆 2024-11-10 21:16:32

非常感谢您的回答。我决定使用Itay的答案来解决这个问题。这是我的解决方案。

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

这是 FileNameFilter,它阻止插入非法字符。这应该适用于 Unix、Windows 和 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 似乎也是一个很好的解决方案,但 Itay 的答案对我来说更简单。非常感谢!

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!

白衬杉格子梦 2024-11-10 21:16:32

InputVerifier很好地补充了 JFormattedTextField,如此处所示。

InputVerifier complements JFormattedTextField nicely, as seen here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文