2012-10-12 19 views
5

Mam tutaj kod, który dostałem z bloga MDP. sizefilter i filtr liczbowy. w jaki sposób ustawić pole tekstowe, aby ustawić filtr dla dwóch filtrów dokumentów.Jak zrobić pole tekstowe z dwoma filtrami dokumentów

Tutaj 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); 
} 
} 

ten kod jest dla 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(); 
} 
} 

Odpowiedz

3

masz dwie opcje o ile widzę. Albo utworzyć filtr kompozytowy, który wykonuje iteracje nad każdym z filtrów:

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); 
     } 
    } 
} 

Wy prawdopodobnie chcesz instancję kompozyt z filtrem bardziej restrykcyjnej pierwszy, więc chcesz skonstruować go tak:

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

Jeśli zamówienie jest niezwykle ważne, możesz rozważyć przepisanie swoich filtrów jako dekoratorów, np przekazać drugi filtr do pierwszego, a następnie wywołać go.

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

Próbowałem pierwszy kod i naprawiono niektóre błędy importu. i tam ten jeden błąd w tej części: 'filter.replace (fb, offs, długość, a);' to jest błąd: ** metoda zamień w klasie javax.swing.text.DocumentFilter nie może być zastosowany do podanych typów wymagany jest: javax.swing.text.DocumentFilter.FilterBypass, int, int, java.lang.String, javax.swing.text.AttributeSet Znaleziono: javax.swing.text.DocumentFilter.FilterBypass, int, int, javax.print.attribute.AttributeSet ** –

+1

Zaimportowano nieprawidłowy AttributeSet. –

+1

teraz, że połączyłem dwa filtry to po prostu nie działa dobrze nie wiem dlaczego. ale w jakiś sposób ogranicza pole tekstowe tylko do znaków, a po przekroczeniu limitu mogę tylko wpisywać liczby –