Java JTextComponent convert input to upper case
import java.awt.BorderLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; class MyKeyListener extends KeyAdapter { public void keyTyped(KeyEvent evt) { JTextComponent c = (JTextComponent) evt.getSource(); char ch = evt.getKeyChar(); if (Character.isLowerCase(ch) == false) { return;/* w w w. j av a 2s. co m*/ } try { c.getDocument().insertString(c.getCaretPosition(), "" + Character.toUpperCase(ch), null); evt.consume(); } catch (BadLocationException e) { } } } public class Main { public static void main(String[] argv) throws Exception { JTextField component = new JTextField(); component.addKeyListener(new MyKeyListener()); JFrame f = new JFrame(); f.add(component, BorderLayout.NORTH); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300, 300); f.setVisible(true); } }