Writing a Java Swing application to display Japanese characters

January 30, 2007

I have seen enough content on the net to create a web application that supports asian languages input and display.

You can find a good article here : http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/

On the other hand, I couldnot find comprehensive example on achieving the same using rich clients such as a Swing application.

Some help from the internet and effort later, I was successful at writing a Swing application to display Japanese characters read from a .properties file. Some learnings from this exercise are:

  • First and foremost, you need to install fonts that support the language you are trying to use. For e.g you need a Japanese font if you intend to run the application on a English version of Windows. You can download one(MS Mincho) from : http://www.themeworld.com/cgi-bin/redir.pl/fonts/msmincho.zip
  • On Windows, this needs to be installed under the /windows/fonts folder.
  • The font is the only thing that needs to be installed on your client machine to run the application. Using the font is lots easier in Swing unlike in AWT. For AWT components i.e one that has a native peer, you need to customize the settings of the JRE i.e modify font.properties under /jre/lib to include the font you have installed under each font type. I have intentionally not provided the details as it applies to AWT and not Swing, the subject of this post.
  • Now in your Swing application, you just need to set the font of the Swing component before setting its text.
  • Now forthe source of the text. It can come from a text file such as a .properties file. Note here though that the file must contain the content in ascii form and not in unicode form. You may use the native2ascii tool that comes with Java 2 SDK to achieve the conversion.

I have attached a sample Swing application that displays Japanese text from a .properties file. It is available as Editor.doc (rename to Editor.java)

You also need the .properties file. It is available as messagesbundle_ja_jp.doc (rename to messagesbundle_ja_jp.properties)

Copy both files to a single folder. You can then run the application as:

javac -classpath . Editor.java

java -classpath . Editor ja JP

You will then be able to see the application as shown below:

Japanese Swing app

About these ads

29 Responses to “Writing a Java Swing application to display Japanese characters”

  1. ettelie Says:

    Hi,
    I’m trying to write a program that will help me memorize japanese words and kanji. Your post will hepl me a lot, Thank you :}

    • Lenin Says:

      Can’t find resource for bundle java.util.PropertyResourceBundle, key Title

      When i run program (i.e,what you suggest) it will return the above error,how can i resolve the above problem


  2. Ettelie,
    Good to know it is of help to somebody and the person acknowledges it :)

  3. Homero Says:

    It’s a poorshit. The Java programa must run in English Windows Version.

    Don’t fuck me!!


  4. Homero,

    It DOES run on English Windows version. The screen shot you have seen here is from a Windows XP SP2 machine.
    The only thing you need is the Japanese font installed.

    Would appreciate if you keep the profanities out.

  5. Roy Says:

    The Java program only runs in english Windows version. This is very limited.

  6. Sinhue Says:

    Pues si, esa madre solo corre en Windows en español, le quita la portabilidad a Java.

  7. Basilio Says:

    No Sinhue, the Java program runs only in English Windows version however this is trash.

    Regards


  8. All,
    The sample provided above is an illustrative example. It was not tested on all operating systems and/or locales. It was never meant to. Please check your own JVM’s codepage or equivalent. I dont expect the code to change much in principle for different locales.

  9. Suresh Says:

    Hi,

    Thanks a million. Got the Java GUI working in Japanese. Also thanks for putting a link to the font. Saved the effort of searching it :-) I later changed the font to Microsoft’s Gothic though.

  10. Peter Gragert Says:

    Auch bei mir auf einem Vista PC funktioniert es!

    Nach langem (na ja nicht so lange ;-) hat mir Ihr Artikel mit den Beispielen die Augen geoeffnet.

    Danke!
    Peter

  11. Peter Gragert Says:

    The chinesecomputing link is broken ;-(


  12. Peter,
    Thanks. Yes, the link to the AWT settings page does appear to be broken. I have modified the post to simply mention that the font.properties of the JRE needs to be modified to include the Asian font for use in AWT. I did not detail it as it may confuse readers and lead them to believe that the changes are needed for Swing as well, which in fact is not necessary.


  13. Peter,

    Ich Kenne nur ein bischen Deutsch und Ich kann nicht verstehen was sie sagen hier :)
    I used translating tools to kind of figure out what you said – Danke!

  14. vardhan Says:

    i want ot display Telugu on swing what should i do


  15. Vardhan,

    You essentially follow the same approach described above. What you need is a Java font that supports the Telugu characters though. All Indian languages use 2 bytes per character, just like Japanese or Chinese.

  16. siva Says:

    Hi Regu,
    Thanks. i want to display applet in russion language,every thing like menus,buttons are displaying in that except titles.Titles are displaying question marks(????) instead russian.Any suggesions..plz..Its very urgent to me..Thanks in advance.
    siva


  17. Siva,

    I suspect it is due to the fact that while rest of the components (like menus, buttons e.t.c) are true Swing components i.e. do not have an AWT peer, the Frame/Window of the applet is an AWT component and therefore must be handled that way. See my post above on what you might have to do (customize the settings of the JRE) in order to display such characters in AWT.

  18. siva Says:

    Thanks for such fast reply..and i’ll try in that way.

    Regards,
    Siva.

  19. siva Says:

    Hi Regu,
    As you suggested,i tried to customize the settings of JRE,but i didn’t find any solutions.my environment is i am using express server as DB server for database and 9ias server as Application server, and gui is applets using swings.all menus and buttons displaying in russian except title(displaying question marks).plz help me in this regard.plz tell me how to customise my JRE.
    Thanks,
    siva.

  20. siva Says:

    Hi Regu,
    i am sending one simple java programme,which is displaying its Title properly in jdk1.4 but not in jdk1.1(i am using jdk1.1.8.16),plz help me to display in jdk1.1,actually i want it in 1.1.the programe is as follows..

    import java.awt.*;
    public class Center extends Frame {

    static String text[];
    private Dimension dim;
    static public void main (String args[]) {
    if (args.length == 0) {
    System.err.println (“Usage: java Center “);
    return;
    }

    String[] s=Toolkit.getDefaultToolkit().getFontList();
    for(int i=0;i<s.length;i++)
    System.out.println(s[i]);
    // text = args;
    args[0]=”\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438″;
    text=args;
    Center f = new Center();
    f.show();
    }
    public void addNotify() {
    super.addNotify();
    int maxWidth = 0;
    FontMetrics fm = getToolkit().getFontMetrics(getFont());
    for (int i=0;i<text.length;i++) {
    maxWidth = Math.max (maxWidth, fm.stringWidth(text[i]));
    }
    Insets inset = insets();
    dim = new Dimension (maxWidth + inset.left + inset.right,
    text.length*fm.getHeight() + inset.top + inset.bottom);
    resize (dim);
    setTitle(“\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438″);
    System.out.println(“\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438″);
    }
    public void paint (Graphics g) {
    g.translate(insets().left, insets().top);
    FontMetrics fm = g.getFontMetrics();
    for (int i=0;i<text.length;i++) {
    int x,y;
    x = (size().width – fm.stringWidth(text[i]))/2;
    y = (i+1)*fm.getHeight()-1;
    g.drawString (text[i], x, y);
    }
    }
    }

    Thanks,
    Siva.

  21. siva Says:

    Hi,

    In your programe insert the follwing code

    setTitle(messages.getString(“Title”));

    then also result is same..plz give me solution with jdk 1.3 or lower versions.

    Thanks in advance,
    Siva.

  22. Danny Says:

    This is my first time writing a Swing application, and I was having a hard time figuring out why my Japanese resource bundles weren’t showing up, but the native2ascii conversion fixed the issue.

    Quick question, I’ve used resource bundles in web applications before, and I was able to use the UTF-8 properties files directly, without converting using native2ascii (at least that’s what I remember). Is there a reason it works for web apps and not for swing apps? Is it because the display in web applications is using web browsers which can be configured to view utf-8 directly?

  23. Srikanth Says:

    Regu, Thanks for the post. It helped me to fix font display issue in my swing app.

  24. Rahul Dev Says:

    Thnaks Ragu for such a detailed process in displaying any language text in

    I understood the procees.I could not able get what font I should set before setting the text (may be in case of japaneese).

    Awaiting for your reply

  25. Rahul Dev Says:

    Hi Ragu ,

    As earlier said in your post , I did all the below steps.
    I downloaded and installed tft file in /windows/fonts folder.

    Set the font before setting the text

    Used u’r messagesbundle_ja_jp.properties in the src folder

    Could able to suceed .Could please tell me where I am doing wrong.

  26. Acallseet Says:

    большой сайт накрутка вконтакте, купить подписчиков вконтакте, накрутка followers.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: