简体   繁体   中英

java force JTextField to be uppercase only

有没有办法强制JTextField所有用户输入在Java中是大写的?

a complete working example may help you

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

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 UpperCasedTextFieldTester extends JFrame {
    /** */
    private static final long serialVersionUID = -4767854098431909437L;

    public UpperCasedTextFieldTester(){
        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 UpperCasedTextFieldTester().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);
    }
}

You have two immediate choices. You can supply your own document , which is little bit of work, or supply your own DocumentFilter

Or, you just google and see what you find, like http://www.java2s.com/Code/Java/Swing-JFC/DocumentFilterthatmapslowercaseletterstouppercase.htm this for example ;)

Below is an example of DocumentFilter .

AbstractDocument document = (AbstractDocument) textfield
                .getDocument();
        document.setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset,
                    String string, AttributeSet attr)
                    throws BadLocationException {
                super.insertString(fb, offset, string.toUpperCase(), attr);
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length,
                    String text, AttributeSet attrs)
                    throws BadLocationException {
                super.insertString(fb, offset, text.toUpperCase(), attrs);
            }

        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM