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: