如何从JTextField中用户键盘输入中过滤非法/禁止的文件名字符?
|
我想过滤用户的键盘输入中JTextField中非法/禁止的文件名字符。我已经在JTextField中设置了大写过滤器。
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);
}
}
我该如何解决这个问题?
没有找到相关结果
已邀请:
4 个回复
蓄荣糖些
氮顺
-在此处和此处查看 格式化的文本字段为开发人员提供了一种方法,可以指定可以在文本字段中键入的有效字符集
冕偷淮款
这是FileNameFilter,它阻止插入非法字符。这应该可以在Unix,Windows和Mac OS中使用。
JFormattedTextField似乎也是一个很好的解决方案,但是Itay的答案对我来说更简单。非常感谢你!
田损比报
与
很好地互补。