DocumentFilter that maps lowercase letters to uppercase : Document Event « Swing JFC « Java






DocumentFilter that maps lowercase letters to uppercase

DocumentFilter that maps lowercase letters to uppercase
   
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);
  }

  //no need to override remove(): inherited version allows all removals

  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, java.awt.BorderLayout.CENTER);
    frame.getContentPane().add(jtf, java.awt.BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 120);
    frame.setVisible(true);
  }
}

           
         
    
    
  








Related examples in the same category

1.An extension of PlainDocument that restricts the length of its contentAn extension of PlainDocument that restricts the length of its content
2.Document Event DemoDocument Event Demo
3.React to the document update insert changed eventReact to the document update insert changed event
4.HTMLDocument: Document Iterator Example
5.DocumentListener DemoDocumentListener Demo
6.extends PlainDocument to create a float value type text field
7.extends PlainDocument to create alpha and numeric value based field field
8.Implement a document cleaner that maps lowercase letters to uppercaseImplement a document cleaner that maps lowercase letters to uppercase
9.Document ElementIterator DemoDocument ElementIterator Demo
10.Format JTextField's text to uppercase
11.Overriding a Few Default Typed Key Bindings in a JTextComponent
12.Overriding - key
13.Override $ key
14.Overriding space key
15.Disable a character so that no action is invoked.
16.Bind a keystroke to shift-space
17.Prevent the space from being inserted when shift-space is pressed.
18.Subclass InputVerifier
19.extends PlainDocument
20.This Document restricts the size of the contained plain text to the given number of characters.
21.A frame with three text fields to set the background color.