Implement a document cleaner that maps lowercase letters to uppercase : Document « Swing Event « Java Tutorial






import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;


public class Main{
  public static void main(String[] args) {
    DocumentFilter dfilter = new UpperCaseFilter();

    JTextArea jta = new JTextArea();
    ((AbstractDocument) jta.getDocument()).setDocumentFilter(dfilter);

    JFrame frame = new JFrame("UpcaseFilter");
    frame.getContentPane().add(jta, java.awt.BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 120);
    frame.setVisible(true);
  }

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

}








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