0

I have created a program that reads from a JSON file in UTF-8 like so:

JSONObject a = (JSONObject) parser.parse(new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)));

After I read the file, I want to print some rows to a JTable. Problem is, I get boxes as output instead of the corresponding characters (e.g. Korean or "ကာ လီ ကို" for the wanted output of the image).

image

How can I solve this issue?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
el diablo
  • 35
  • 8
  • Have you tried adding -Dfile.encoding=utf-8 when you launch your jar? – Pilpo Aug 23 '21 at 13:36
  • Could you give me an example how to do that? – el diablo Aug 23 '21 at 13:39
  • Yes you should try with the jvm option: java -jar yourjar.jar -Dfile.encoding=utf-8 – Pilpo Aug 23 '21 at 13:39
  • didn't work :/ still getting the boxes – el diablo Aug 23 '21 at 13:42
  • 3
    Use a font that displays Korean characters. The default Swing font does not. – Gilbert Le Blanc Aug 23 '21 at 13:46
  • @GilbertLeBlanc my problem isn't only Korean. I want to be able to print out any given language. I tried Sans and again it didn't work – el diablo Aug 23 '21 at 13:48
  • When you are debugging, does `JSONObject a` contains the desired character ? – Pilpo Aug 23 '21 at 13:49
  • yes the JSON file does contain all the desired characters. When I print them out in Intellij they print fine but I read it is because Intellij uses their own way of encoding/decoding – el diablo Aug 23 '21 at 13:51
  • 2
    *I want to be able to print out any given language.* In that case, you need to ship a big unicode font with many glyphs with your app – g00se Aug 23 '21 at 13:56
  • @g00se I am using the following code `jt=new JTable(tableModel); jt.setFont(new Font("Arial Unicode MS", Font.PLAIN, 16));` to include the font. I guess Arial Unicode MS is not enough. Any suggestions? – el diablo Aug 23 '21 at 14:01
  • Well you can test that by using something like a `JLabel` with that font set. Say `label.setText("\ud5d8");` – g00se Aug 23 '21 at 14:18
  • 1
    *"Any suggestions?"* Well as @g00se suggested, supply a `Font` *that **can*** display the characters. If relying on inbuilt fonts, use [`Font.canDisplayUpTo(String)`](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/java/awt/Font.html#canDisplayUpTo(java.lang.String)) to find suitable fonts. E.G. as see in [this answer](https://stackoverflow.com/a/42335976/418556). – Andrew Thompson Aug 23 '21 at 14:20
  • That's a good idea, but I must confess I've had mixed results. For instance, (I was using `Font.canDisplay` but have now tried using `Font.canDisplayUpTo`) I'm told that I have nothing that can display the Korean `\ud5d8` yet am looking at that very character in my terminal – g00se Aug 23 '21 at 14:26
  • @AndrewThompsonI'm crying it worked for a few languages but broke a few others (like Arabic). It was a great idea though. – el diablo Aug 23 '21 at 14:28
  • 1
    I’m voting to close this question because there is no problem here. Unless you implemented OS-level font stack fall-through, characters that aren't in any of the font(s) used cannot be displayed, the solution is the obvious "bundle more typefaces if you need to support languages not currently supported by the ones you already have". But remember that fonts are not free, even if they come with your OS: _bundling fonts to which you do not have distribution rights is quite illegal_. – Mike 'Pomax' Kamermans Aug 24 '21 at 18:10

0 Answers0