Unicode display problem in JList

Hi everyone,

I'm trying to display chess figurines in a Swing JList. I want to use the unicode characters (as described here: http://en.wikipedia.org/wiki/Figurine_notation) to compose a String. As for now, in my JList I always have a square at the place my chess figurines should be. I know it is normally because the font does not support the caracters, I have tried Tahoma, Lucida Sans Unicode, and so on and still nothing. I've looked into Character Map at the specified location and I can't seem to find them, but they print alright in my browser using HTML entities. I've:
  myJList.setFont(new Font('Tahoma', Font.PLAIN, 12))
with all the fonts I could think of and still nothing. I'm missing something here. I create the string as follow:
  String s = "\u265F" + "f2-f3";
for example.

I'd like to be able to use a string to represent the movements and I'd like to be able to distribute easily my program to Windows, Mac or Linux so I'd need a font that can be use everywhere (TTF for example) without installation in the OS if possible.

> I know I could also use icons of the figurines but that's not what I'm trying to do here.

So, where am I missing something? As someone ever printed those unicode chars in their Java program somewhere?

Thanks for your help!
LVL 3
loop_untilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

udo_borkowskiCommented:
Have you tried to use the "well defined fonts":

- SansSerif
- Serif
- Monospaced

?

0
loop_untilAuthor Commented:
Just tried:

  listMoves.setFont(new Font("SansSerif", Font.PLAIN, 12));

Still a big white square.

Any more ideas?
0
udo_borkowskiCommented:
Probably you should use a "full Unicode font" like "Arial Unicode MS".

Please check if you have this installed on your machine.

(see also http://en.wikipedia.org/wiki/Arial_Unicode_MS)

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

loop_untilAuthor Commented:
'Arial Unicode MS' is a font only distributed with Microsoft Office, I could not freely distribute it with my program. And I don't understand why it currently prints well in my browser if I don't have the correct font installed already?
0
CEHJCommented:
>>Probably you should use a "full Unicode font" like "Arial Unicode MS".

That font certainly doesn't work for me - nor does Tahoma

>>And I don't understand why it currently prints well in my browser

Do you mean the table at the top of that Wiki page (the bottom is an image)?
0
udo_borkowskiCommented:
Maybe it is using "Lucida Sans Unicode", one of the other common Unicode fonts. It should be installed with newer windows versions. Have a try.

0
CEHJCommented:
>>Lucida Sans Unicode

No. No dice. My *guess* is that it'd have to be a pretty major Unicode font as those symbols would be pretty low priority for support.
0
udo_borkowskiCommented:
At least with the "Arial Unicode MS" font (that is installed on my machine) the black chess pawn is displayed correctly, using the following code:
---------------
            DefaultListModel model = new DefaultListModel();
            model.addElement("\u265F" + "f2-f3");

            JList list = new JList(model);
            list.setFont(new Font("Arial Unicode MS", Font.PLAIN,24));
---------------
      
0
CEHJCommented:
Interesting - not for me though. What about the others?
0
loop_untilAuthor Commented:
>> Do you mean the table at the top of that Wiki page (the bottom is an image)?

I mean the table at the top. I also recreated a simple html file on my computer, without any font specification (not in CSS nor in HTML) so it uses the default browser font and it displays correctly.

As of Arial Unicode MS, I dont have the font and that won't really solve my problem as it is Microsoft and they rarely gives things away, so there might be licence problem.

They stated Tahoma in the Wikipedia link I gave, does it work for anyone of you guys?
0
udo_borkowskiCommented:
Tahoma did not worked for me.
0
loop_untilAuthor Commented:
Glad to see you back CEHJ (well, I'm the one who's back but...)

>> My *guess* is that it'd have to be a pretty major Unicode font as those symbols would be pretty low priority for support.

I think you are right about this but as it seems to be a supported HTML entities (Firefox), I wonder if it's not included somewhere in peoples computer...

Oh! While typing that message, I just tested the Wikipedia page in Explorer and it does not displays correctly. While Firefox does... Anyone knows what unicode support Firefox has?
0
udo_borkowskiCommented:
>> Anyone knows what unicode support Firefox has?

A good one, as it seems.

BTW: I am also using Firefox and had the same idea as you. I search the Firefox installation for some truetype fonts but didn't find one.
0
CEHJCommented:
>>
BTW: I am also using Firefox and had the same idea as you. I search the Firefox installation for some truetype fonts but didn't find one.
>>

Are you saying that Firefox is displaying chess OK for you?

>>does it work for anyone of you guys?

Nope
0
loop_untilAuthor Commented:
Yes, Firefox is displaying okay for me (and seems okay for udo_borkowski too). But not IE.

Seems Firefox has it own (and very good) unicode support.


I don't know if using a HTML component to display the moves would work... I know Swing has a simple HTML component but I'll have to try to see if it displays correctly (I doubt).
0
CEHJCommented:
>>Seems Firefox has it own (and very good) unicode support.

 The question is, then, where is it coming from? If it hasn't installed fonts in the system and it doesn't seem to have installed them in it own install tree, then where are they coming from?
0
CEHJCommented:
While that's being pondered, give this a run and see what happens:


import java.awt.*;

public class CanDisplay {
      static String currentFontName = "";

      public static void main(String[] args){

            if(args.length < 1){
             System.out.println("Usage: java CanDisplay <Unicode character, e.g. 0044>");
             System.exit(-1);
            }
            char c = (char)Integer.parseInt(args[0],16);
            System.out.println("Checking on character " + c);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            Font[] fonts = ge.getAllFonts();

            for(int i = 0;i < fonts.length ;i++){
                  String fontName = fonts[i].getFontName();
                  /**
                   * Assume that if a glyph is contained in a certain typeface,
                   * then its first font will be able to display it. Otherwise
                   * this iteration can take a long time.
                   */
                  if (fontName.equals(currentFontName)){
                        continue;
                  }
                  else {
                        currentFontName = fontName;
                  }
                  if (fonts[i].canDisplay(c)){
                        System.out.println(fonts[i]);
                        // Just take the first one as this iteration can take a long time!
                        System.exit(0);
                  }
            }
            System.out.println("Font not found");
      }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
udo_borkowskiCommented:
>> I know Swing has a simple HTML component but I'll have to try to see if it displays correctly (I doubt).

I already tested that. It doesn't work.

But that brought me to another idea. If you just want you feature of displaying chess figures and don't "insist" in using Unicode, try this:

- Create an image for every chess figure (i.e. 12 images, maybe taken as screenshots from the wiki page).
- Put these images as resource beside your class
- Use the following code to display the image and the text in a list entry.

            URL url = MyClass.class.getResource("blackpawn.jpg");
            String s = "<html><img src='"+url+"'>f2-f3</html>";
            DefaultListModel model = new DefaultListModel();
            model.addElement(s);
            JList list = new JList(model);
.
0
udo_borkowskiCommented:
CEHJ: I used your code and tested it with a "chess figure"

I removed the exit to see all fonts and here is what I got:

java.awt.Font[family=Arial Unicode MS,name=Arial Unicode MS,style=plain,size=1]
java.awt.Font[family=Bitstream Cyberbit,name=Bitstream Cyberbit,style=plain,size=1]
java.awt.Font[family=MS Mincho,name=MS Mincho,style=plain,size=1]

Both MS fonts displayed the figure. The Cyberbit figure as just "a white eagle on a white flag" (i.e just white).



0
udo_borkowskiCommented:
Guys, it was fun working with you, but now I have to leave until tomorrow. It is already nearly 2:00 here in Germany.

loop_until: nice riddle ;-)
0
CEHJCommented:
That's weird - i could have sworn i had Arial Unicode MS, but apparently not. I have none of the other two either

>>I removed the exit

Yes, another 'non single exit point' - i might get wrapped across the knuckles ;-)
0
loop_untilAuthor Commented:
Thanks CEHJ, with your little snippet I was able to found I could use 'MS Gothic' on my computer. Well, at the moment this is not very portable: I'd like it to run on Linux and Mac also but I'll see later about that. Close enought. I guess I could make this piece of code search quickly at the start for a valid font.

Thanks for the help.
0
loop_untilAuthor Commented:
udo_borkowski, thanks also for your help, but for the HTML entities, this is not part of the question, I had specifically said:

  > I know I could also use icons of the figurines but that's not what I'm trying to do here.

Anyway, I didn't know we could use some HTML in a JList. I'll raise the points a little and share with you for giving a couple of ideas, I like it when a lot of ideas come along! ;-)
0
CEHJCommented:
>>I guess I could make this piece of code search quickly at the start for a valid font.

Yes, but to be certain you've really got to be prepared to install your own font or use images as Udo suggested
0
loop_untilAuthor Commented:
I guess if it displays sometimes a "White Eagle" that I will not be able to make a snippet to detect a valid font... Well, at least I was able to display on my computer. Well, personnal work should be more fun than stress, so I'll think of something else for distributing my little game.

0
CEHJCommented:
Actually using html entities is a bit irrelevant as the system can only render them as it renders anything else. This is more related to an en/decoding scenario.

Interestingly though, characters that refuse to render normally in a browser can sometimes be forced to appear by making them entities - don't ask me why
0
loop_untilAuthor Commented:
I'm going to distribute the points so you're not tie here for the rest of your life but if you ever have a great idea for having the unicode chess figurines available OS-wide easily, I'll be glad to hear from it.

Have a nice evening (night) everyone!
0
CEHJCommented:
>>I guess if it displays sometimes a "White Eagle"

That's an odd one - mistake in font or decoding (more likely the former). That doesn't affect the detection logic you've got in mind (the Unicode character set is certainly fixed)
0
CEHJCommented:
8-)
0
udo_borkowskiCommented:
First:
I found the following page that lists "free" Unicode fonts: http://www.slovo.info/unifonts.htm


Second:
I tried the Code2000 font (http://home.att.net/~jameskass/CODE2000.ZIP).

It is a shareware font you can try for free. "If you like it and use it after a reasonable evaluation period, you are obliged to register the font by sending me $5 (Five U.S. Dollars, or equivalent in any currency.)" (http://www.slovo.info/code2000.htm)

AND IT WORKS! That font also contains chess figures (not just "white eagles")


Third:
 8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.