extends DocumentFilter : DocumentFilter « javax.swing.text « Java by API






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

           
       








Related examples in the same category

1.DocumentFilter.FilterBypass: insertString(int offset, String string, AttributeSet attr)
2.DocumentFilter.FilterBypass: replace(int offset, int length, String string, AttributeSet attrs)
3.extends DocumentFilter (Interger filter)
4.extends DocumentFilter (Upper case filter)