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

--