Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8521977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:58:40+00:00 2026-06-11T06:58:40+00:00

I’m working on a Java application for people learning German, and I have run

  • 0

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.

My attempts so far:

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' && evt.isAltGraphDown()){
      setText(getText() + "ö");
      evt.consume();
    }
  }


}

Code above does not work (GermanTextField behaves like standard JTextField), and when I print evt.getKeyChar() to console this is what I get:

?
?
?
?

This may be due to my own language, because ALT + o produces ó on my system. Of course I could have done it like that:

  public void keyTyped(KeyEvent evt) {
    if(evt.getKeyChar() == 'ó'){
      setText(getText() + "ö");
      evt.consume();
    }
  }

But it probably won’t work on any systems other than Polish.

My question is: is there any solution to this problem that will behave as expected on systems with different language settings?


Full solution to this problem, based on MvGs answer:

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<Integer, String> transform = 
      new HashMap<Integer, String>();
  
  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, "äÄ");
    transform.put(KeyEvent.VK_U, "üÜ");
    transform.put(KeyEvent.VK_O, "öÖ");
    
    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();
    }
  }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:58:41+00:00Added an answer on June 11, 2026 at 6:58 am

    To identify key events independent of the current locale, don’t use getKeyChar. Instead, use isKeyCode() to identify the key independent of the character associated with it. Like this:

    if (evt.getKeyCode() == KeyEvent.VK_O && evt.isAltGraphDown())
    

    This should match Alt Gr + O on any keyboard layout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.