Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unicode display problem in JList

Posted on 2004-12-01
30
Medium Priority
?
858 Views
Last Modified: 2013-12-03
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!
0
Comment
Question by:loop_until
  • 11
  • 10
  • 9
30 Comments
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12719913
Have you tried to use the "well defined fonts":

- SansSerif
- Serif
- Monospaced

?

0
 
LVL 3

Author Comment

by:loop_until
ID: 12719946
Just tried:

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

Still a big white square.

Any more ideas?
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12720070
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:loop_until
ID: 12720618
'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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12721160
>>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
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12721269
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12721293
>>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
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12721604
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12721628
Interesting - not for me though. What about the others?
0
 
LVL 3

Author Comment

by:loop_until
ID: 12721770
>> 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
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12721782
Tahoma did not worked for me.
0
 
LVL 3

Author Comment

by:loop_until
ID: 12721795
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
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12721897
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12721973
>>
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
 
LVL 3

Author Comment

by:loop_until
ID: 12722024
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12722110
>>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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12722121
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
 
LVL 3

Assisted Solution

by:udo_borkowski
udo_borkowski earned 200 total points
ID: 12722135
>> 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
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12722185
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
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12722207
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12722208
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
 
LVL 3

Author Comment

by:loop_until
ID: 12722215
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
 
LVL 3

Author Comment

by:loop_until
ID: 12722237
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12722238
>>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
 
LVL 3

Author Comment

by:loop_until
ID: 12722248
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12722252
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
 
LVL 3

Author Comment

by:loop_until
ID: 12722257
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12722263
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12722272
8-)
0
 
LVL 3

Expert Comment

by:udo_borkowski
ID: 12724111
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question