2012-10-12 5 views
5

Tôi có một mã ở đây mà tôi đã nhận dạng weblog của MDP. bộ lọc sizefilter và bộ lọc số. làm thế nào để tôi làm cho một textfield đặt bộ lọc của nó cho hai bộ lọc tài liệu.Làm thế nào để làm cho textfield có 2 bộ lọc tài liệu

Đây isthe numberfilter

import javax.swing.text.BadLocationException; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.DocumentFilter; 

public class IntFilter extends DocumentFilter { 

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

    StringBuffer buffer = new StringBuffer(string); 
    for (int i = buffer.length() - 1; i >= 0; i--) { 
     char ch = buffer.charAt(i); 
     if (!Character.isDigit(ch)) { 
      buffer.deleteCharAt(i); 
     } 
    } 
    super.insertString(fb, offset, buffer.toString(), attr); 
} 

public void replace(DocumentFilter.FilterBypass fb, 
        int offset, int length, String string, AttributeSet attr) throws BadLocationException { 
    if (length > 0) fb.remove(offset, length); 
    insertString(fb, offset, string, attr); 
} 
} 

mã này là dành cho sizefilter

import java.awt.*; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DocumentFilter; 

public class SizeFilter extends DocumentFilter { 

private int maxCharacters;  

public SizeFilter(int maxChars) { 
    maxCharacters = maxChars; 
} 

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) 
     throws BadLocationException { 

    if ((fb.getDocument().getLength() + str.length()) <= maxCharacters) 
     super.insertString(fb, offs, str, a); 
    else 
     Toolkit.getDefaultToolkit().beep(); 
} 

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) 
     throws BadLocationException { 

    if ((fb.getDocument().getLength() + str.length() 
      - length) <= maxCharacters) 
     super.replace(fb, offs, length, str, a); 
    else 
     Toolkit.getDefaultToolkit().beep(); 
} 
} 

Trả lời

3

Bạn đã có hai tùy chọn như xa như tôi có thể nhìn thấy. Hoặc tạo ra một bộ lọc tổng hợp mà lặp trên mỗi bộ lọc:

public class CompositeFilter extends DocumentFilter { 
    private final DocumentFilter[] filters; 

    public CompositeFilter(DocumentFilter... filters) { 
     this.filters = filters; 
    } 

    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) 
     throws BadLocationException { 
     for (DocumentFilter filter : filters) { 
      filter.insertString(fb, offs, str, a); 
     } 
    } 

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) 
     throws BadLocationException { 
     for (DocumentFilter filter : filters) { 
      filter.replace(fb, offs, length, a); 
     } 
    } 
} 

Bạn có thể muốn nhanh chóng tổng hợp với bộ lọc hạn chế hơn trước, vì vậy bạn muốn xây dựng nó như vậy:

new CompositeFilter(new SizeFilter(10), new IntFilter()); 

Nếu đơn đặt hàng là cực kỳ quan trọng, bạn có thể xem xét viết lại các bộ lọc của bạn như trang trí, ví dụ vượt qua bộ lọc thứ hai vào đầu tiên và sau đó gọi nó.

public class SizeFilter extends DocumentFilter { 
    private int maxCharacters;  
    private final DocumentFilter delegate; 

    public SizeFilter(int maxChars, DocumentFilter delegate) { 
     maxCharacters = maxChars; 
     this.delegate = delegate; 
    } 

    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) 
     throws BadLocationException { 

     if ((fb.getDocument().getLength() + str.length()) <= maxCharacters) 
      delegate.insertString(fb, offs, str, a); 
     else 
      Toolkit.getDefaultToolkit().beep(); 
    } 

    public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) 
     throws BadLocationException { 

     if ((fb.getDocument().getLength() + str.length() - length) <= maxCharacters) 
      delegate.replace(fb, offs, length, str, a); 
     else 
      Toolkit.getDefaultToolkit().beep(); 
     } 
    } 
} 
+2

tôi đã thử mã đầu tiên và sửa một số lỗi nhập. và có lỗi này ở phần này: 'filter.replace (fb, offs, length, a);' đây là lỗi: ** phương thức thay thế trong lớp javax.swing.text.DocumentFilter không thể áp dụng cho các loại bắt buộc: javax.swing.text.DocumentFilter.FilterBypass, int, int, java.lang.String, javax.swing.text.AttributeSet tìm thấy: javax.swing.text.DocumentFilter.FilterBypass, int, int, javax.print.attribute.AttributeSet ** –

+1

Bạn đã nhập sai AttributeSet. –

+1

bây giờ mà tôi kết hợp hai bộ lọc nó chỉ không hoạt động đúng i dunno lý do tại sao. nhưng nó bằng cách nào đó giới hạn trường văn bản chỉ với các ký tự và vượt quá giới hạn tôi chỉ có thể gõ số –