Java by API/javax.swing.text/DocumentFilter

Материал из Java эксперт
Версия от 14:12, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DocumentFilter.FilterBypass: insertString(int offset, String string, AttributeSet attr)

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class MainClass {
  public static void main(String[] args) {
    JTextField field = new JTextField(30);
    ((AbstractDocument) (field.getDocument())).setDocumentFilter(new DocumentFilter() {
      public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
          throws BadLocationException {
        System.out.println("insert");
        fb.insertString(offset, string.toUpperCase(), attr);
      }
      public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr)
          throws BadLocationException {
        System.out.println("replace");
        fb.replace(offset, length, string.toUpperCase(), attr);
      }
    });
    JFrame frame = new JFrame("User Information");
    frame.getContentPane().add(field);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





DocumentFilter.FilterBypass: replace(int offset, int length, String string, AttributeSet attrs)

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class MainClass {
  public static void main(String[] args) {
    JTextField field = new JTextField(30);
    ((AbstractDocument) (field.getDocument())).setDocumentFilter(new DocumentFilter() {
      public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
          throws BadLocationException {
        System.out.println("insert");
        fb.insertString(offset, string.toUpperCase(), attr);
      }
      public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr)
          throws BadLocationException {
        System.out.println("replace");
        fb.replace(offset, length, string.toUpperCase(), attr);
      }
    });
    JFrame frame = new JFrame("User Information");
    frame.getContentPane().add(field);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





extends DocumentFilter

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class MainClass {
  public static void main(String[] args) {
    JTextField field = new JTextField(30);
    ((AbstractDocument) (field.getDocument())).setDocumentFilter(new DocumentFilter() {
      public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
          throws BadLocationException {
        System.out.println("insert");
        fb.insertString(offset, string.toUpperCase(), attr);
      }
      public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr)
          throws BadLocationException {
        System.out.println("replace");
        fb.replace(offset, length, string.toUpperCase(), attr);
      }
    });
    JFrame frame = new JFrame("User Information");
    frame.getContentPane().add(field);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





extends DocumentFilter (Interger filter)

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
public class MainClass {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Range Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 2));
    frame.add(new JLabel("Range: 0-255"));
    JTextField textFieldOne = new JTextField();
    Document textDocOne = textFieldOne.getDocument();
    DocumentFilter filterOne = new IntegerRangeDocumentFilter(0, 255);
    ((AbstractDocument) textDocOne).setDocumentFilter(filterOne);
    frame.add(textFieldOne);
    frame.setSize(250, 150);
    frame.setVisible(true);
  }
}
class IntegerRangeDocumentFilter extends DocumentFilter {
  int minimum, maximum;
  int currentValue = 0;
  public IntegerRangeDocumentFilter(int minimum, int maximum) {
    this.minimum = minimum;
    this.maximum = maximum;
  }
  public void insertString(DocumentFilter.FilterBypass fb, int offset, String string,
      AttributeSet attr) throws BadLocationException {
    if (string == null) {
      return;
    } else {
      String newValue;
      Document doc = fb.getDocument();
      int length = doc.getLength();
      if (length == 0) {
        newValue = string;
      } else {
        String currentContent = doc.getText(0, length);
        StringBuffer currentBuffer = new StringBuffer(currentContent);
        currentBuffer.insert(offset, string);
        newValue = currentBuffer.toString();
      }
      currentValue = checkInput(newValue, offset);
      fb.insertString(offset, string, attr);
    }
  }
  public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
      throws BadLocationException {
    Document doc = fb.getDocument();
    int currentLength = doc.getLength();
    String currentContent = doc.getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + after;
    currentValue = checkInput(newValue, offset);
    fb.remove(offset, length);
  }
  public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
      AttributeSet attrs) throws BadLocationException {
    Document doc = fb.getDocument();
    int currentLength = doc.getLength();
    String currentContent = doc.getText(0, currentLength);
    String before = currentContent.substring(0, offset);
    String after = currentContent.substring(length + offset, currentLength);
    String newValue = before + (text == null ? "" : text) + after;
    currentValue = checkInput(newValue, offset);
    fb.replace(offset, length, text, attrs);
  }
  private int checkInput(String proposedValue, int offset) throws BadLocationException {
    int newValue = 0;
    if (proposedValue.length() > 0) {
      try {
        newValue = Integer.parseInt(proposedValue);
      } catch (NumberFormatException e) {
        throw new BadLocationException(proposedValue, offset);
      }
    }
    if ((minimum <= newValue) && (newValue <= maximum)) {
      return newValue;
    } else {
      throw new BadLocationException(proposedValue, offset);
    }
  }
}





extends DocumentFilter (Upper case filter)

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public 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);
  }
  public static void main(String[] args) {
    DocumentFilter dfilter = new UpcaseFilter();
    JTextArea jta = new JTextArea();
    JTextField jtf = new JTextField();
    ((AbstractDocument) jta.getDocument()).setDocumentFilter(dfilter);
    ((AbstractDocument) jtf.getDocument()).setDocumentFilter(dfilter);
    JFrame frame = new JFrame("UpcaseFilter");
    frame.getContentPane().add(jta, "Center");
    frame.getContentPane().add(jtf, "South");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 120);
    frame.setVisible(true);
  }
}