开发者

Malformed Farsi characters on AWT

开发者 https://www.devze.com 2023-01-02 01:32 出处:网络
When I started programming with the JDK6, I had no problem with text components, neither in AWT nor in Swing.

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 s开发者_高级运维how ;
    // 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...


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

Malformed Farsi characters on AWT

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号