German characters in JTextField

I am working on a Java application for people learning German, and I have a problem with special characters in this language. I want to create a subclass of JTextField that will interpret ALT + a as ä, ALT + o as ö, etc., At the same time, as usual, for all ASCII characters.

My attempts:

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(); } } } 

The code above does not work ( GermanTextField behaves like a standard JTextField), and when I print evt.getKeyChar() for the 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 do it like this:

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

But it probably will not 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?


A complete 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(); } } } 
+6
source share
3 answers

To identify key events that are independent of the current locale, do not 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.

+6
source

This may be due to my own language, because ALT + o produces - on my system. Of course, I could do it like this:

use DocumentFilter for JTextComponents

But it probably will not 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?

  • no no

  • to hope that all PCs get the imputed correct value for Locale on Native OS (wrong solution)

  • you can write any Unicode Chars using ALT and numbers

  • the most secure is only customization for Locale , then you can create an array of characters for a specific Locale (own Encode Page )

+1
source

The problem is that JTextField uses a different default font than JTextArea. I had the same problem in an application that I wrote that was supposed to support multiple languages.

The cause of your problem is that JTextField is usually used to display a font at a single spacing, such as Courier New. Typically, Java does not contain additional mappings for a single-spaced font for displaying Kanji.

You have work to do with the fix because the font with the name "123" is missing, therefore it is used by default (dialog). The dialog font is internally mapped to the font family in the font.properties file of your platform. This will be the same font that JTextField uses.

I have the following fix to ensure that the same font definition is used in all graphic components. You can also find a specific key for JTextField and change it. Thus, you do not need to worry about the fonts of any component, they will be initialized by the dialog. Enter the following code inside your class containing the JTextField.

 Object fontDefinition = new UIDefaults.ProxyLazyValue("javax.swing.plaf.FontUIResource", null, new Obje java.util.Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object value = UIManager.get(key); if (value instanceof javax.swing.plaf.FontUIResource) { UIManager.put(key, fontDefinition); } } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/925365/


All Articles