Localize a JFileChooserTag(s): Internationalization Swing


Modern Swing release have now built-in ready-to-use translations for the JFileChooser. The language is choosen based on the current Locale. So you don't have to do anything to display the JFileChooser in the right language.

The user interface elements provided by the J2SE Runtime Environment 5.0, include Swing dialogs, messages written by the runtime environment to the standard output and standard error streams, as well as messages produced by the tools provided with the JRE. These user interface elements are localized into the following languages:

Language Locale ID
Chinese (Simplified)zh_CN
Chinese (Traditional)zh_TW
Englishen
Frenchfr
Germande
Italianit
Japaneseja
Koreanko
Spanishes
Swedishsv
Sun Supported Locales

This example will show 2 radio buttons, one for english, one for french. Press the button to display a localized JFileChooser according to the radio button selected.

Create 2 properties files, one for english , one for french (these files are incomplete but should be enough to get you started).

[JFileChooser_en.properties]

Title=Real's JFileChooser
lookInLabelText=Current
filesOfTypeLabelText=File type
upFolderToolTipText=go up

[JFileChooser_fr.properties]

Title=JFileChooser de R\u00e9al
lookInLabelText=Courant
filesOfTypeLabelText=Type de fichier
upFolderToolTipText=Remonte

Then

[LocalizeJFileChooser.java]

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;


public class LocalizeJFileChooser extends JPanel
   implements ActionListener {
   JButton go;
   AbstractButton button;
   ButtonGroup group;
   Locale locale;
   String msg ;

    protected JFileChooser z_chooser;
    String z_choosertitle;

  public LocalizeJFileChooser() {
    group = new ButtonGroup();

    locale = Locale.US;  // default value
    button = new JRadioButton("English", true);
    button.setActionCommand("en");
    button.addActionListener(this);
    group.add(button);
    add(button);

    button = new JRadioButton("Francais");
    button.setActionCommand("fr");
    button.addActionListener(this);
    group.add(button);
    add(button);

    go = new JButton("Do it");
    go.addActionListener(this);
    add(go);

    locale = Locale.US;

   }

 public void setUILanguage() {
   ResourceBundle rb;
   rb = ResourceBundle.getBundle("JFileChooser", locale);

   z_choosertitle = rb.getString("Title");

   UIManager.put
     ("FileChooser.lookInLabelText", 
        rb.getString("lookInLabelText"));
   UIManager.put
     ("FileChooser.filesOfTypeLabelText", 
        rb.getString("filesOfTypeLabelText"));
   UIManager.put
     ("FileChooser.upFolderToolTipText", 
        rb.getString("upFolderToolTipText"));
 /*
      do the same with :
                    FileChooser.fileNameLabelText
                    FileChooser.homeFolderToolTipText
                    FileChooser.newFolderToolTipText
                    FileChooser.listViewButtonToolTipTextlist
                    FileChooser.detailsViewButtonToolTipText
                    FileChooser.saveButtonText=Save
                    FileChooser.openButtonText=Open
                    FileChooser.cancelButtonText=Cancel
                    FileChooser.updateButtonText=Update
                    FileChooser.helpButtonText=Help
                    FileChooser.saveButtonToolTipText=Save
                    FileChooser.openButtonToolTipText=Open
                    FileChooser.cancelButtonToolTipText=Cancel
                    FileChooser.updateButtonToolTipText=Update
                    FileChooser.helpButtonToolTipText=Help

      Almost all Swing widgets can be customize this way. You can
      examine the Swing sources to get these values or check
      http://www.gargoylesoftware.com/papers/plafdiff.html for
      a list of them.

*/

   }

  public void actionPerformed(ActionEvent e) {
    int result;

    if (e.getSource() instanceof JRadioButton) {
      if (e.getActionCommand().equals("en"))
         locale = Locale.US;
      else
         locale = Locale.FRANCE;
      setUILanguage();
      }
    else {
        z_chooser = new JFileChooser();
        z_chooser.setCurrentDirectory(new java.io.File("."));
        z_chooser.setDialogTitle(z_choosertitle);
        if (z_chooser.showOpenDialog(this) !=
              JFileChooser.APPROVE_OPTION)
            return;
        }
     }

  public Dimension getPreferredSize(){
    return new Dimension(200, 200);
    }

  public static void main(String s[]) {
    JFrame frame = new JFrame("");
    LocalizeJFileChooser panel = new LocalizeJFileChooser();
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
          }
        }
      );
    frame.getContentPane().add(panel,"Center");
    frame.setSize(panel.getPreferredSize());
    frame.setVisible(true);
    }
}

blog comments powered by Disqus