OverrideKey2

JTextField component = new JTextField();
component.addKeyListener(new MyKeyListener());

public class MyKeyListener extends KeyAdapter {
    public void keyTyped(KeyEvent evt) {
        JTextComponent c = (JTextComponent)evt.getSource();
        char ch = evt.getKeyChar();

        if (Character.isLowerCase(ch)) {
            try {
                c.getDocument().insertString(
                    c.getCaretPosition(), ""+Character.toUpperCase(ch), null);
                evt.consume();
            } catch (BadLocationException e) {
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.