5

When I started programming with the JDK6, I had no problem with text components, neither in AWT nor in Swing.

But for labels or titles of AWT components I do have a problem. I can't display Farsi characters on AWTs components (in Swing I type them into the source code).

Here's my sample code:

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Properties;

public class EmptyFarsiCharsOnAWT extends JFrame{

public EmptyFarsiCharsOnAWT() {
    super("مثال");
    setDefaultCloseOperation(3);
    setVisible(rootPaneCheckingEnabled);
}
public static void main(String[] args) throws AWTException, IOException {
    JFrame jFrame = new EmptyFarsiCharsOnAWT();
    MenuItem show ;
    // approach 1 = HardCoding :
    /*
    show = new MenuItem("\u0646\u0645\u0627\u06cc\u0634");
     *
     */

    // approach 2 = using simple utf-8 saved text file :
    /*
        BufferedReader in = new BufferedReader(new FileReader("farsiLabels.txt"));
        String showLabel = in.readLine();
        in.close();

        show = new MenuItem(showLabel);
     *
     */

     // approach 3 = using properties file :

    FileReader in = new FileReader("farsiLabels.properties");
    Properties farsiLabels = new Properties();
    farsiLabels.load(in);
    show = new MenuItem(farsiLabels.getProperty("tray.show"));
    PopupMenu popUp = new PopupMenu();
    popUp.add(show);

        // creating Tray object
        Image iconIamge = Toolkit.getDefaultToolkit().getImage("greenIcon.png");

        TrayIcon trayIcon = new TrayIcon(iconIamge, null, popUp);
        SystemTray tray = SystemTray.getSystemTray();
        tray.add(trayIcon);

        jFrame.setIconImage(iconIamge);
    } 
}

These three approaches all work when run with an IDE, but when I make a JAR containing this class (by means of NetBeans > project > clean & build), I don't see the expected characters (it shows EMPTY/BLANK SQUARES)!

Note: It seems I can not attach anything, so the contents of the text file would be this: نمایش and the contents of properties file:

#Sun May 02 09:45:10 IRDT 2010   
tray.show=نمایش

And I think I have to let you know that I posted this question a while ago on SDN and "the Java Ranch" forums and other native forums and still I'm waiting...

By the way I am using latest version of Netbeans IDE...

I will be grateful if anybody has a solution to these damn AWT components never rendering any Farsi character for me...

Jasper
  • 2,166
  • 4
  • 30
  • 50
jlover2010
  • 71
  • 6
  • dear trashgod, i definitely like to trigger up that flag, but seems because i am a tyro here, i can not, but promise to do that as soon as i got enough reputations to do that (I'm not familiar with this site very much...) Thank you – jlover2010 May 30 '10 at 20:11
  • Interesting. I can't up-vote your answer; somehow the system thinks it's mine, perhaps due to the salutation. – trashgod May 30 '10 at 21:21
  • Yes, that was it. I've up-voted your question and answer. – trashgod May 30 '10 at 21:23

2 Answers2

3

I suspect that this is platform related. Your example appears to work on my platform using approach 1 in either Netbeans or the command line; I didn't try the other approaches.

There might be a disparity between the IDE and the command line with regard to the default character encoding. I've noticed that NetBeans, Eclipse and many consoles can be set to something other than the platform default. Here's code to check:

System.out.println(System.getProperty("file.encoding"));
System.out.println(Charset.defaultCharset().name());

You might look at this related question, too.

Addendum: Note show string changed to match the JFrame title for comparison. The title and menu look the same from NetBeans' Run > Run Project as well as via these command lines:

$ java -cp build/classes EmptyFarsiCharsOnAWT
$ java -jar dist/test6.jar

image

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class EmptyFarsiCharsOnAWT extends JFrame{

public EmptyFarsiCharsOnAWT() {
    super("مثال");
    setDefaultCloseOperation(3);
    setVisible(rootPaneCheckingEnabled);
}
public static void main(String[] args) throws AWTException, IOException {
    JFrame jFrame = new EmptyFarsiCharsOnAWT();
    MenuItem show ;
    // approach 1 = HardCoding :
    show = new MenuItem("\u0645\u062b\u0627\u0644");
    PopupMenu popUp = new PopupMenu();
    popUp.add(show);
        // creating Tray object
        Image iconIamge = Toolkit.getDefaultToolkit().getImage("image.jpg");
        TrayIcon trayIcon = new TrayIcon(iconIamge, null, popUp);
        SystemTray tray = SystemTray.getSystemTray();
        tray.add(trayIcon);
        jFrame.setIconImage(iconIamge);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Do you note i am talking off the IDE, when the JAR file is made from this class? or you just check approach1 from IDE (or command line also) because the problem is after packing up the archive (as i mentioned in my question, after the codes) if you got the right result from JAR, i will amaze! Also the result of both encoding checks are "utf-8" THX – jlover2010 May 29 '10 at 15:56
  • If it's not the encoding it may be the font; it looks OK on Mac OS X and Ubuntu. – trashgod May 29 '10 at 18:39
  • @jlover2010: I hope you are not trying to make me post something naughty in Farsi. :-) – trashgod May 30 '10 at 18:09
  • No, absolutely not. you can check that word in the below online dictionary page : http://www.aryanpour.com/ Thank you for attaching the picture, Well, if the code is that exactly below the picture, i just can be happy there is at least one person in the world who got the right result! – jlover2010 May 30 '10 at 19:56
  • I meant it as a joke, symbolized by `:-)`, but I appreciate the dictionary reference. Sorry I don't know XP; maybe NetBeans has the right font. The code's the same; the image is http://en.wikipedia.org/wiki/File:Mona_Lisa.jpg – trashgod May 30 '10 at 21:16
  • I know about the smiley :), mostly I'd like to mention a reference to you for future...(the link is really a good one).I know the MonaLiza too ;) That's a good point mentioning the Netbeans, so I'll search it in my machine today and if i got the right result, i will mark your answer as accepted...fair i think I hope to resolve – jlover2010 May 31 '10 at 12:04
2

The most exciting part of your reply was: "$ java -jar dist/test6.jar" ! Does it really shows the real characters (just like the frame title)?! and not boxes or garbage ? I'm sorry if I believe it hard, because the only problem in my developing work with Java took such long without any answer nor from searching, nor asking in forums is this!

So, what can I do? what font should I use? Unfortunately I'm not so familiar with fonts, until now I've just used global fonts in Java (Serif,SansSerif,etc.) and only modified their size or style, but after you suggest I examined several Persian ttf fonts through these codes:

        File fontFile = new File("F_JADID.TTF");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        show.setFont(font.deriveFont(15f));

but just boxes was the result! (just using HardCoding) I think i should mention that my envirounment is win xp and i have this problem not only in my machine, but another running xp os too. And I'm using jdk6u17.

I can be agree with you in suspecting the fonts, because encoding problem (in my experience) appears with question mark, but garbage or empty boxes related to rendering characters. But still i have the problem, just like the first day :( What font you use and another question i encountered is: Why swing doesn't have any problem without specifying the font, but AWT.

Addendum: Thanks to Oscar Reyes in this page for giving this link and thanks to StackOverflow :) They saved me! from this section i should quote:

An application using peered AWT components can only use logical font names.

and from this section should quote:

For applications using AWT peered components, Sun's JREs select fonts for Chinese, Japanese, or Korean only when running on host operating systems localized for these specific languages

Yes, you guess right! by setting the OS locale to Farsi, i got the right result.

but i still should research and see how is it possible to have the right result by not setting the right locale, from that article.

I will explain how, when i got the result, but still will listen to here. wish me luck.

Community
  • 1
  • 1
jlover2010
  • 71
  • 6