Gordon
Gordon

Reputation: 4843

Capitalize all letters in a Textfield in Java

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).

Upvotes: 8

Views: 21341

Answers (6)

Naiguel Santos
Naiguel Santos

Reputation: 1

A help for friends who find it interesting: how to make letters written in TextField capitalized. Ex: Legend:

txtCadastrarNome = name of the variable of text field.

txtCadastrarNomeKeyTyped = action when it is being typed from the keyboard.

private void txtCadastrarNomeKeyTyped(java.awt.event.KeyEvent evt) { 
txtCadastrarNome.setText(txtCadastrarNomeCliente.getText().toUpperCase());
}

Upvotes: -1

user2266204
user2266204

Reputation: 21

Try

jTextField.addKeyListener(new KeyAdapter() {

  public void keyTyped(KeyEvent e) {
    char keyChar = e.getKeyChar();
    if (Character.isLowerCase(keyChar)) {
      e.setKeyChar(Character.toUpperCase(keyChar));
    }
  }

});

Upvotes: 2

SOLIHIN
SOLIHIN

Reputation: 1

Try

private void inText_UserIDKeyReleased( java.awt.event.KeyEvent evt ) {
    String UsrID=inText_UserID.getText().toUpperCase();
    inText_UserID.setText( UsrID );
}

Upvotes: -1

corsiKa
corsiKa

Reputation: 82589

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

Upvotes: 1

Glennular
Glennular

Reputation: 18225

Format JTextField's text to uppercase

Uses DocumentFilter

or

How to Use Formatted Text Fields

Uses MaskFormatter

Upvotes: 9

Daniel
Daniel

Reputation: 28104

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

Upvotes: 1

Related Questions