Why could a Java Swing program not display German characters such as umlauts (ä, ö, ...)?

Viewed 241

The Swing program shows wrong characters instead of German umlauts. This button should be "Schließen", for example: enter image description here. This occurs for all UI elements as far as I can see.

The code to create the UI is nothing unusual, for example:

about = new JButton("");
about.setToolTipText("Über das Programm");

I already checked following things:

  1. The encoding of the .java files is utf-8 (checked with VS code)
  2. The font is javax.swing.plaf.FontUIResource[family=Dialog,name=Dialog,style=bold,size=12]. Dialog appears to be the default font, so it would be strange if it did not support umlauts. But I have not found a way to check this, because when I check my installed fonts through windows I don't find a font called "Dialog".

The project also uses gradle (6.8).

What could be a reason for this behaviour? When I talked to somebody who compiled the same program on Linux they didn't seem to have this problem.

Edit:

I also found code such as this:

JLabel test = new JLabel();
test.setFont(new Font("Arial", Font.PLAIN, 12));
test.setText("<html>Something with ß</html>");

Arial surely supports umlauts, but this code still did not display the ß correctly

1 Answers

Parse source code as UTF-8

Thanks to the commenters I could get to the bottom of the issue: The compiler was not parsing my source code as UTF-8 character encoding.

I had to add following to my build.gradle file:

compileJava {
    options.encoding = 'UTF-8'
}
Related