How do I format JTextField's text to uppercase?

Category: javax.swing, viewed: 10190 time(s).

The process to change the JTextField's text to some other format like upper case for instance can be easily done by adding a DocumentFilter to the text field component. The DocumentFilter allow us to do a filter action for changes in the document such as insert, replace and remove.

In this example we describe how to do it.

package org.kodejava.example.swing;

import javax.swing.text.DocumentFilter;
import javax.swing.text.BadLocationException;
import javax.swing.text.AttributeSet;
import javax.swing.text.AbstractDocument;
import javax.swing.*;
import java.awt.*;

public class DocumentFilterExample extends JFrame {
    public DocumentFilterExample() throws HeadlessException {
        initComponents();
    }

    protected void initComponents() {
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().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);

        getContentPane().add(firstName);
        getContentPane().add(lastName);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new DocumentFilterExample().setVisible(true);
            }
        });
    }

    class UppercaseDocumentFilter extends DocumentFilter {
        //
        // Override insertString method of DocumentFilter to make the text format
        // to uppercase.
        //
        public void insertString(DocumentFilter.FilterBypass fb, int offset,
                                 String text, AttributeSet attr)
                throws BadLocationException {

            fb.insertString(offset, text.toUpperCase(), attr);
        }

        //
        // Override replace method of DocumentFilter to make the text format
        // to uppercase.
        //
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
                            String text, AttributeSet attrs)
                throws BadLocationException {

            fb.replace(offset, length, text.toUpperCase(), attrs);
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats