• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unicode in Swing ?  RSS feed

 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've been struggling to display unicode characters in JEditorPane unsuccessfully. So i tried a simple test class to display unicode character in JButton and JLabel, and still fails, although the System.out.println works :roll:



About the third label from above, i've tried this simple html in firefox, and it shows correctly a dos boxing character:


How to show these unicode characters in Swing ?
Any help 'd be appreciated ..


Albert Kam
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's strange. It works fine for me on OS X, showing a little box for both javaLabel and htmlLabel.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, are the boxes correct? I got little boxes on Ubuntu, too.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Albert Kam:
I've been struggling to display unicode characters in JEditorPane unsuccessfully. So i tried a simple test class to display unicode character in JButton and JLabel, and still fails, although the System.out.println works


Java is unicode-friendly (you can even use unicode characters in your variable/method/class names so long as your editor can handle the encoding) and Swing is no exception.

It sounds to me like a font issue. Try setting your labels' font to one you are sure contains the unicode characters you are trying to use.

[btw, what is the point of doing new JLabel("<html>葉</html>") instead of just new JLabel("葉") ?]
 
Paul Clapham
Sheriff
Posts: 22375
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Oh, are the boxes correct? I got little boxes on Ubuntu, too.
The boxes are what you get when your font can't render a particular character.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
The boxes are what you get when your font can't render a particular character.

Getting a box would be the desired outcome, since Unicode character 250F is a little box (as opposed to the big box for missing characters).
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, what I got didn't look like 250f. If however I print (char)0x250f I get the little angle thing shown previously.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried a few more options and I am afraid I didn't get the angle thing. Sorry.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:

Getting a box would be the desired outcome, since Unicode character 250F is a little box


Unicode 250F is BOX DRAWINGS HEAVY DOWN AND RIGHT, which is not a little box.

If the font used by your console output panel is different from the font used by your JLabel, then it shouldn't be too surprising if the character looks different in the console vs. in the GUI.

Does theLabel.getFont().canDisplay('\u250F') shed any light?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it does. "canDisplay('\u250f')" resolutely prints "false."
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Yes, it does. canDisplay('\u250f') resolutely prints false.


Then, as I suggested above, you will need to set a better font. You might find some candidates here.
 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Sorry i didnt realize this before, but the first post contains error. Actually the line :

is not the real code, the character shows up because of browser rendering. I was using this instead of the chinese character. It's & # 9 4 8 7 ; without the spaces. I was hoping that the html renderer in swing could render the unicode into a real character.

The same with the example from html, it was <html><body>& # 9 4 8 7 ;</body></html>, again without the spaces >.<

Thanks for the input, but how about the JEditorPane not showing the html unicode ? If i'm not mistaken, the HtmlEditorKit used by JEditorPane only supports html 3.2. Is displaying unicode characters already supported in html 3.2 and JEditorPane?

Anyway, i'm gonna try the fonts suggested here.

Be right back

Cheers,

[ January 07, 2008: Message edited by: Albert Kam ]
 
Albert Kam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeahh .. at last ! So happy !

Try this font :
http://dejavu.sourceforge.net

Good Luck ! :-)
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Albert Kam:
Be right back [ January 07, 2008: Message edited by: Albert Kam ]
We got there in the end . 11 out of 10 for persistence
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!