How to filter illegal/forbidden filename characters from user's keyboard input in JTextField?
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 (of开发者_JAVA百科fset, 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?
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 ...
}
}
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
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!
InputVerifier
complements JFormattedTextField
nicely, as seen here.
精彩评论