title: German characters in JTextField
body: <p>I'm working on a Java application for people learning German, and I have run into a problem with the special characters of this language. I want to make a subclass of JTextField that will interpret ALT + a as ä, ALT + o as ö and so on, while behaving as usual for all ASCII characters.</p>
  <p>My attempts so far:</p>
  <pre><code>public class GermanTextField extends JTextField implements KeyListener{
    public GermanTextField() {
      init();
    }
     
    // other constructors ...
    
    private void init() {
      addKeyListener(this);
    }
  
    
    
    public void keyPressed(KeyEvent arg0) {}
  
  
    public void keyReleased(KeyEvent arg0) {}
  
  
    public void keyTyped(KeyEvent evt) {
      if(evt.getKeyChar() == 'o' &amp;&amp; evt.isAltGraphDown()){
        setText(getText() + &quot;ö&quot;);
        evt.consume();
      }
    }
  
  
  }
  </code></pre>
  <p>Code above does not work (<code>GermanTextField</code> behaves like standard JTextField), and when I print <code>evt.getKeyChar()</code> to console this is what I get:</p>
  <pre><code>?
  ?
  ?
  ?
  </code></pre>
  <p>This may be due to my own language, because ALT + o produces ó on my system. Of course I could have done it like that:</p>
  <pre><code>  public void keyTyped(KeyEvent evt) {
      if(evt.getKeyChar() == 'ó'){
        setText(getText() + &quot;ö&quot;);
        evt.consume();
      }
    }
  </code></pre>
  <p>But it probably won't work on any systems other than Polish.</p>
  <p>My question is: is there any solution to this problem that will behave as expected on systems with different language settings?</p>
  <hr>
  <h1>Full solution to this problem, based on MvGs answer:</h1>
  <pre><code>package daswort.gui;
  
  import java.awt.event.KeyEvent;
  import java.awt.event.KeyListener;
  import java.util.HashMap;
  import java.util.Map;
  
  import javax.swing.JTextField;
  
  public class GermanTextField extends JTextField implements KeyListener{
    
    private Map&lt;Integer, String&gt; transform = 
        new HashMap&lt;Integer, String&gt;();
    
    public GermanTextField() {
      init();
    }
  
  
    public GermanTextField(int columns) {
      super(columns);
      init();
    }
  
  
    public GermanTextField(String text, int columns) {
      super(text, columns);
      init();
    }
  
  
    public GermanTextField(String text) {
      super(text);
      init();
    }
  
    
    private void init() {
      transform.put(KeyEvent.VK_A, &quot;äÄ&quot;);
      transform.put(KeyEvent.VK_U, &quot;üÜ&quot;);
      transform.put(KeyEvent.VK_O, &quot;öÖ&quot;);
      
      addKeyListener(this);
    }
  
    
    
    public void keyPressed(KeyEvent evt) {
      if(evt.isAltGraphDown()){
        String umlaut = transform.get(evt.getKeyCode());
        if(umlaut != null){
          int idx = evt.isShiftDown() ? 1 : 0;
          setText(getText() + umlaut.charAt(idx));
        }
      }
    }
  
    public void keyReleased(KeyEvent arg0) {}
  
  
    public void keyTyped(KeyEvent evt) {
      if(evt.isAltGraphDown()){
        evt.consume();
      }
    }
  
  
  }
  </code></pre>