tags:

views:

116

answers:

3

Is it to possible to capitalize the letters in a Textfield as they are being typed by the user in Java?

E.g. The user would type 'hello' and 'HELLO' would appear in the Textfield.

(Odd request and I don't like the idea either).

A: 

ModifyListener and getText().toUpperCase() are your friends.

Daniel
Make sure you test typing in the middle of the text.
BCS
+4  A: 

Format JTextField's text to uppercase

Uses DocumentFilter

or

How to Use Formatted Text Fields

Uses MaskFormatter

Glennular
A: 

This is probably an inefficient way to do it

but you could have a section in your KeyTyped event handler

if(event.getSource() == capitalTextArea) {
    String text = capitalTextArea.getText();
    if(Character.isLowerCase(text.charAt(text.length()-1))) {
        capitalTextArea.setText(text.toUpperCase());
    }
}

I might have syntatical mistakes, but that's the apporach i'd take

glowcoder
I think you will have trouble with cursor positioning if you do this. It is also much less efficient than the DocumentFilter way.
DJClayworth
You might be right about the cursor positioning. You're definitely right about there being more efficient ways to do it.
glowcoder