Custom Document Filter : Document « Swing Event « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing Event » Document 
15.12.1.Custom Document FilterPrevious/Next
Custom Document Filter
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.Document;
import javax.swing.text.DocumentFilter;

class IntegerRangeDocumentFilter extends DocumentFilter {

  public IntegerRangeDocumentFilter() {

  }

  public void insertString(DocumentFilter.FilterBypass fb, int offset, String string,
      AttributeSet attrthrows BadLocationException {
    System.out.println("insert string"+ string);
    System.out.println(offset);
    super.insertString(fb, offset, string, attr);
  }

  public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
      throws BadLocationException {
    System.out.println("remove");

    
    super.remove(fb, offset, length);
  }

  public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
      AttributeSet attrsthrows BadLocationException {
    System.out.println("replace");
    super.replace(fb, offset, length, text, attrs);
  }
}

public class RangeSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Range Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textFieldOne = new JTextField();
    Document textDocOne = textFieldOne.getDocument();
    DocumentFilter filterOne = new IntegerRangeDocumentFilter();
    ((AbstractDocumenttextDocOne).setDocumentFilter(filterOne);
    frame.add(textFieldOne);

    frame.setSize(250150);
    frame.setVisible(true);
  }
}
15.12.Document
15.12.1.Custom Document FilterCustom Document Filter
15.12.2.extends PlainDocument to create a float value type text field
15.12.3.extends PlainDocument to create alpha and numeric value based field field
15.12.4.Element WalkerElement Walker
15.12.5.Implement a document cleaner that maps lowercase letters to uppercase
15.12.6.Limit JTextField input to a maximum length
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.