views:

109

answers:

2

Hi As i started programming by jdk6, i had no problem in text components neither in awt nor in swing. But for labels or titles of awt components, yes : I couldn't have Farsi characters displayable on AWTs just as simple as Swing by typing them into the source code. lets check this SSCCE :

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);
    } 
}  

Yes, i know each of three approaches in source code does right when you may test it from IDE , but if you make a JAR contains just this class, by means of NetBeans > project > clean&build ,you won't see the expected characters and will just get EMPTY/BLANK SQUARES !

Unfortunately, opposed to other situations i encountered before, here there is no way to avoid using awt and make use of Swing in this case. And this was just an SSCCE i made to show the problem and my recent (also first) application suffers from this subject.

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

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

but i don't think by giving you the unicode-scape sequence, these would be necessary any way...

And i think should have to let you know I posted this question a while ago in SDN and "the Java Ranch" forums and other native forums and still I'm watching...

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

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

Thanks in advance

+1  A: 

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

http://i47.tinypic.com/mx1vvd.png

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
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
If it's not the encoding it may be the font; it looks OK on Mac OS X and Ubuntu.
trashgod
@jlover2010: I hope you are not trying to make me post something naughty in Farsi. :-)
trashgod
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
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
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
A: 

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,...) 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 :)

jlover2010