Format JTextField's text to uppercase : Document Event « Swing JFC « Java






Format JTextField's text to uppercase

    
 
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;

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 Main extends JFrame {
  public Main() throws HeadlessException {
    setSize(200, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    DocumentFilter filter = new UppercaseDocumentFilter();

    JTextField firstName = new JTextField();
    firstName.setPreferredSize(new Dimension(100, 20));
    ((AbstractDocument) firstName.getDocument()).setDocumentFilter(filter);

    JTextField lastName = new JTextField();
    lastName.setPreferredSize(new Dimension(100, 20));
    ((AbstractDocument) lastName.getDocument()).setDocumentFilter(filter);

    add(firstName);
    add(lastName);
  }

  public static void main(String[] args) {
    new Main().setVisible(true);
  }

}

class UppercaseDocumentFilter 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 attrs) throws BadLocationException {

    fb.replace(offset, length, text.toUpperCase(), attrs);
  }
} 

   
    
    
    
  








Related examples in the same category

1.DocumentFilter that maps lowercase letters to uppercaseDocumentFilter that maps lowercase letters to uppercase
2.An extension of PlainDocument that restricts the length of its contentAn extension of PlainDocument that restricts the length of its content
3.Document Event DemoDocument Event Demo
4.React to the document update insert changed eventReact to the document update insert changed event
5.HTMLDocument: Document Iterator Example
6.DocumentListener DemoDocumentListener Demo
7.extends PlainDocument to create a float value type text field
8.extends PlainDocument to create alpha and numeric value based field field
9.Implement a document cleaner that maps lowercase letters to uppercaseImplement a document cleaner that maps lowercase letters to uppercase
10.Document ElementIterator DemoDocument ElementIterator Demo
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.