Thursday, March 6, 2008

Internationalize Swing's JFileChooser in Unicode (Bangla)

Recently I had to develop a small demo application for one of the leading newspapers in Bangladesh. I decided to localize my application and see all messages in Bangla. Searching Google I came across this article “Internationalization for Swing standard components”. This set me off in the right direction. However, there were some little tweaks I had to perform to get Bangla working. Firstly I had to set the font for the components user in JFileChooser.

final Font FONT_SOLAIMAN_LIPI = new Font("SolaimanLipi", Font.PLAIN, 15);
final FontUIResource eFontUIResource = new FontUIResource(FONT_SOLAIMAN_LIPI);
UIManager.put("Button.font", eFontUIResource);
UIManager.put("ToolTip.font", eFontUIResource);
UIManager.put("Label.font", eFontUIResource);
UIManager.put("EditorPane.font", eFontUIResource);
UIManager.put("TableHeader.font", eFontUIResource);


Next I had to set message values for properties as mentioned in Adrian's blog. However I had to set a couple of extra properties also

UIManager.put("FileChooser.directoryOpenButtonText");
UIManager.put("FileChooser.directoryOpenButtonToolTipText");


Finally there was this trouble of changing the tooltip for the home directory. The default JFileChooser implementation sets the tooltip to the name of the home directory which in my case (as will be in most cases :) ) was in english. So I had to end up writing a small visitor that would find the home folder button and reset the tooltip of the button as follows:

final String eHomeFolderName = eFileChooser.getFileSystemView().getHomeDirectory().getName();
MySwingComponentVisitor eVisitor = new MySwingComponentVisitor(eFileChooser, JButton.class) {
@Override
public boolean onComponentVisited(JComponent pVisitedComponent) {
JButton eButtonComponent = (JButton) pVisitedComponent;
if (eHomeFolderName.equals(eButtonComponent.getToolTipText())) {
eButtonComponent.setToolTipText(
getLocalizedMessage("homeFolderToolTipText"));
return STOP_VISITING;
}
return CONTINUE_VISITING;
}
};
eVisitor.visitComponents();


And the results were just so pleasing. A JFileChooser almost entirely in Bangla. Below are images showing how the JFileChooser transformed :)


Click to enlarge
Click to enlarge

--

5 comments :

[hide] Anonymous said...

Hi Shams,

great tips and good work.
I used the ideas which you and the earlier linked page discuss to make
a similar JFileChooser which can switch between different languages as selected by the user. It works a treat.

One remaining problem, when I right click on the Dialog I get a pop up window - in English :-(
and I don't know what keys to set to change the text in the pop up

any ideas?

on May 18, 2008 at 9:41 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @119611421908270889.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

Hi Paul,

You need to do the following:

// Set the fonts for the menu and items
UIManager.put("Menu.font", eFontUIResource);
UIManager.put("CheckBoxMenuItem.font", eFontUIResource);
UIManager.put("RadioButtonMenuItem.font", eFontUIResource);
UIManager.put("MenuItem.font", eFontUIResource);

// And also set the label texts for
FileChooser.viewMenuLabelText
FileChooser.refreshActionLabelText
FileChooser.newFolderActionLabelText
FileChooser.listViewActionLabelText
FileChooser.detailsViewActionLabelText

And you should be able to see your popup in your language too :)

The following link should come in handy:
http://home.tiscali.nl/~bmc88/java/sbook/061.html#radiobuttonmenuitem

Hope it helps.

on May 20, 2008 at 9:28 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5626301244885909789.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Abrahametry Mii said...

Hi Shams,
I have a problem that I wanna to use bangla font in JAVA like Jbuilder4 software(My current project is English to Bangla Dictionary) I can show the E2E but It's impossible to show the Bangla font or type in the JAVA. So how can I add BAngla font or word in JAVA at least to show the word meaning.

One thing If I have to Use UNICODE so how the procedure or implementation pls pls pls show me as soon as possible also with Coding...

My Contacts no 01671118854

And 01721051738

Masud &&& Sajjad

on March 7, 2010 at 1:32 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3795967477714834134.0
  • @Abrahametry Mii
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

Hi Masud,
The nice thing about unicode and java is that you don;t have to do anything special. Just ensure your String in valid Bangla unicode.

In my case, I was using MS Indic IME to allow phonetic typing of Bangla text and reading that in using an XML file in Java.

Hope that helps.

on March 8, 2010 at 6:15 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7782625964880445714.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] David said...

I am having a problem in an old application. It is possible to translate the detail view labels in java 7? For the 'name' label,

UIManager.put("FileChooser.fileNameHeaderText", "Name");

or

UIManager.put("FileChooser.fileNameHeader.textAndMnemonic", "Name");

aren't working... Thanks!

on February 21, 2018 at 7:05 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @38086990952118429.0
  • @David
is the first line of your comment.
Click here to enter your reply