Disturbed Farsi characters on AWT

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

But for AWT component labels or headers I have a problem. I cannot display Farsi characters on AWT components (in Swing I inject them into the source code).

Here is 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 work while working with the IDE, but when I make a JAR containing this class (using NetBeans> project> clean and build), I don't see the expected characters (it shows EMPTY / BLANK SQUARES)!

Note: It seems that I cannot attach anything, so the contents of the text file will be as follows: نمایش and the contents of the properties file:

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

And I think I should tell you that I posted this question some time ago on SDN and Java Ranch forums and other native forums and am still waiting ...

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

I would be grateful if anyone would have a solution to these damned AWT components that never give me any Farsi character ...

+4
source share
2 answers

The most interesting part of your answer was: " $ java -jar dist / test6.jar "! Does this really show the real characters (just like the name of the frame) ?! not boxes or trash? I'm sorry if I find this difficult because the only problem in my evolving work with Java takes so long without any answer, no search, no forum requests!

So what can I do? which font should i use? Unfortunately, I'm not so familiar with fonts, so far I just used global fonts in Java (Serif, SansSerif, etc.) and only changed their size or style, but after you suggested that I study a few 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 only the boxes were the result! (just using HardCoding) I think I should mention that my environment is xp victory, and I have this problem not only on my machine, but also on another xp os launch. And I am using jdk6u17.

I can agree with you, suspecting fonts, because the encoding problem (in my experience) appears with a question mark, but garbage or empty fields related to character rendering. But still, I have a problem, like on the first day :( Which font are you using, and another question that I have encountered: Why the swing does not have problems without specifying the font, but AWT.

Addendum : Oscar Reyes in this page for providing this link and thanks to StackOverflow :) They saved me! from in this section I must indicate:

An application that uses peer-to-peer AWT components can only use logical font names.

and in this section you should indicate:

For applications that use the highly reliable AWT components, Sun JRE selects fonts for Chinese, Japanese, or Korean only when running on host operating systems localized for those specific languages.

Yes, you guess, right! setting the OS locale to Farsi, I got the correct result.

but I still have to research and see how you can get the right result without setting the correct locale from this article.

I will explain how when I get the result, but I will still listen here. I wish you good luck.

+2
source

I suspect this is platform related. Your example runs on my platform using approach 1 in Netbeans or on the command line; I have not tried other approaches.

There may be a mismatch between the IDE and the command line regarding the default character encoding. I noticed that NetBeans, Eclipse, and many consoles can be configured for something other than the default platform. Here is the code to check:

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

You can also look at this related question .

Addendum: The show string note has been modified to fit the JFrame header for comparison. The title and menu look the same from Run > Run Project in NetBeans, as well as from the following 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); } } 
+3
source

Source: https://habr.com/ru/post/1311273/


All Articles