2013-03-01 33 views
0

我使用DocumentFilter将输入限制为整数或小数。 而我在这里发布的代码工作得很好。如何设置输入长度和范围的DocumentFilter?例如1-3或10-80

任何人都可以帮助我如何限制在给定的代码输入长度或范围?

谢谢!

class MyIntFilter extends DocumentFilter { 
public void insertString(FilterBypass fb, int offset, String string, 
    AttributeSet attr) throws BadLocationException { 

    Document doc = fb.getDocument(); 
    StringBuilder sb = new StringBuilder(); 

    sb.append(doc.getText(0, doc.getLength())); 
    sb.insert(offset, string); 

    if (test(sb.toString())) { 
    super.insertString(fb, offset, string, attr); 
    } else { 
    // warn the user and don't allow the insert 
    } 
} 

private boolean test(String text) { 
    try { 
    Integer.parseInt(text); 
    return true; 
    } catch (NumberFormatException e) { 
    return false; 
    } 
} 

@Override 
public void replace(FilterBypass fb, int offset, int length, String text, 
    AttributeSet attrs) throws BadLocationException { 

    Document doc = fb.getDocument(); 
    StringBuilder sb = new StringBuilder(2); 
    sb.append(doc.getText(0, doc.getLength())); 
    sb.replace(offset, offset + length, text); 

    if (test(sb.toString())) { 
    super.replace(fb, offset, length, text, attrs); 
    } else { 
    // warn the user and don't allow the insert 
    } 

} 

@Override 
public void remove(FilterBypass fb, int offset, int length) 
    throws BadLocationException { 
    Document doc = fb.getDocument(); 
    StringBuilder sb = new StringBuilder(2); 
    sb.append(doc.getText(0, doc.getLength())); 
    //sb.append(doc.getText(0, 2)); 
    sb.delete(offset, offset + length); 

    if (test(sb.toString())) { 
    super.remove(fb, offset, length); 
    } else { 
    // warn the user and don't allow the insert 
    } 

} 


} 
+2

你可以只使用'Toolkit.getDefaultToolkit()音()'在其他区块。 – 2013-03-01 21:17:18

+0

或者简单的'JOptionPane'? – 2013-03-01 21:20:40

回答

1

你可能想测试这个(因为我没有),但基本的想法应该让你开始。

还检查了Document Filter Examples

至于设置的最小长度,您可能需要使用InputVerifier以及

class MyIntFilter extends DocumentFilter { 

    private int maxLength = 0; 

    public void setMaxLength(int maxLength) { 
     this.maxLength = maxLength; 
    } 

    public int getMaxLength() { 
     return maxLength; 
    } 

    public void insertString(FilterBypass fb, int offset, String string, 
        AttributeSet attr) throws BadLocationException { 

     Document doc = fb.getDocument(); 
     StringBuilder sb = new StringBuilder(); 

     sb.append(doc.getText(0, doc.getLength())); 
     sb.insert(offset, string); 

     if (maxLength > 0 && doc.getLength() + string.length() <= maxLength) { 
      if (test(sb.toString())) { 
       super.insertString(fb, offset, string, attr); 
      } else { 
       // warn the user and don't allow the insert 
      } 
     } 
    } 

    private boolean test(String text) { 
     try { 
      Integer.parseInt(text); 
      return true; 
     } catch (NumberFormatException e) { 
      return false; 
     } 
    } 

    @Override 
    public void replace(FilterBypass fb, int offset, int length, String text, 
        AttributeSet attrs) throws BadLocationException { 

     Document doc = fb.getDocument(); 
     StringBuilder sb = new StringBuilder(2); 
     sb.append(doc.getText(0, doc.getLength())); 
     sb.replace(offset, offset + length, text); 

     if (test(sb.toString())) { 
      if (sb.length() > maxLength) { 
       length = sb.length() - maxLength; 
       if (length > 0) { 
        text = text.substring(0, length); 
        super.replace(fb, offset, length, text, attrs); 
       } 
      } 
     } else { 
      // warn the user and don't allow the insert 
     } 

    } 

    @Override 
    public void remove(FilterBypass fb, int offset, int length) 
        throws BadLocationException { 
     Document doc = fb.getDocument(); 
     StringBuilder sb = new StringBuilder(2); 
     sb.append(doc.getText(0, doc.getLength())); 
     //sb.append(doc.getText(0, 2)); 
     sb.delete(offset, offset + length); 

     if (test(sb.toString())) { 
      super.remove(fb, offset, length); 
     } else { 
      // warn the user and don't allow the insert 
     } 

    } 
}