Search code examples
javaunicodejythonjtextarea

Unicode characters not being properly displayed in JTextArea


I'm writing a text editor that needs to display and allow edition of Unicode characters. I'm developing it in Jython, since the data model is in Python and the GUI will use Java Swing libraries.

When I load one of these files containing unicode characters, if I print it in my terminal, I get the correct results:

šatti[year]N; n; Ṭebetu[1]MN; mūša[at night]AV; ūm[day]N; n

But when I print it in the JTextArea, I get this instead:

šatti[year]N; n; Ṭebetu[1]MN; mūša[at night]AV; ūm[day]N; n

This is a code snippet that deals with the file reading and displaying in Jython:

textArea = JTextArea()
textArea.font = Font("Monaco", Font.PLAIN, 14)
file = open(filename, "r")
text = file.read()
textArea.setText(text) #gives wrong result in JTextArea
print text             #gives correct result in terminal

I've tried changing the font in the JTextArea so that it's the same as in the terminal, in case that was the problem, but it didn't help. Would a templating library help with this?

I'm not very experienced on working with Unicode, so maybe there is something not obvious to me I should be doing. Any help would be greatly appreciated!


Solution

  • open(filename, "r", "utf-8") as the file is in UTF-8 (multibyte sequences, superset of ASCII)