2

In a Java Swing application, is there a simple way to convert all input to uppercase?

By all input I mean all input in every field of every object everywhere in the application.

Thanks for your help!

Robbie
  • 191
  • 1
  • 9

2 Answers2

4

A complete working example:

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);
    }
}
bugs2919
  • 371
  • 1
  • 8
  • 1
    your answer has been flagged as a low quality answer. please provide some explanations. – Kick Buttowski May 22 '15 at 17:19
  • *"`DocumentFilter`"* Nice one... :) – Andrew Thompson May 22 '15 at 17:19
  • @KickButtowski There's a certain eloquence to 'let the code do the talking'. Well.. code, the [Java Docs](https://docs.oracle.com/javase/8/docs/api/javax/swing/text/DocumentFilter.html) and the [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#filter). – Andrew Thompson May 22 '15 at 17:20
  • 1
    Ooh, but slight tweak `JTextField firstName = new JTextField(); firstName.setPreferredSize(new Dimension(100, 20));` should better be something like `JTextField firstName = new JTextField(20); // suggest a width in columns!` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson May 22 '15 at 17:25
  • Thank you! Thank you! I am new to Java, so I'm learning new things every day! Now I am beginning to understand DocumentFilters! – Robbie May 22 '15 at 17:41
1

If you are receiving the input as a string, you could always do string.toUppercase() method.

Bitcoin M
  • 1,206
  • 8
  • 24