Is there a way to force all user input in a JTextField
to be uppercase in Java?
Asked
Active
Viewed 1.9k times
4

Michael Petrotta
- 59,888
- 27
- 145
- 179

Junba Tester
- 801
- 2
- 9
- 15
-
7Yes, it is possible. What have you tried? – Jeffrey Jul 20 '12 at 01:52
-
4Is there a way to force people posting questions to correctly capitalize Java and the first word of every sentence? – Andrew Thompson Jul 20 '12 at 03:23
-
4is there a way to prevent people from posting useless comments? – Junba Tester Jul 20 '12 at 05:59
-
Unfortunately it's just as hard to prevent folks from posting useless comments as it is to make folks who ask a question to show evidence of prior effort. – Hovercraft Full Of Eels Jul 20 '12 at 10:54
3 Answers
12
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);
}
}

sunil
- 6,444
- 1
- 32
- 44
6
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 ;)

MadProgrammer
- 343,457
- 22
- 230
- 366
-
The OP could also use a `KeyListener` or an `InputVerifier` depending on his needs. – Jeffrey Jul 20 '12 at 02:02
-
@Jeffrey: No, a KeyListener is a low-level solution and should be discouraged. It just feels wrong in that it works on only one aspect of user input when input can come from any number of sources. 1+ to MadProgrammer's answer. I think that a DocumentFilter is the way to go here. – Hovercraft Full Of Eels Jul 20 '12 at 02:05
-
Also, the InputVerifier is called AFTER the text has been added to the field only called when the field loses focus. It's not suitable for this purpose, otherwise you just use a focus listener – MadProgrammer Jul 20 '12 at 02:12
3
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);
}
});

Chan
- 2,601
- 6
- 28
- 45