Is it to possible to capitalize the letters in a Textfield as th开发者_Python百科ey 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).
Format JTextField's text to uppercase
Uses DocumentFilter
or
How to Use Formatted Text Fields
Uses MaskFormatter
Try
jTextField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char keyChar = e.getKeyChar();
if (Character.isLowerCase(keyChar)) {
e.setKeyChar(Character.toUpperCase(keyChar));
}
}
});
ModifyListener
and getText().toUpperCase()
are your friends.
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
Try
private void inText_UserIDKeyReleased( java.awt.event.KeyEvent evt ) {
String UsrID=inText_UserID.getText().toUpperCase();
inText_UserID.setText( UsrID );
}
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());
}
精彩评论