1

Is it possible to localize JOptionPane Message Title which is "Message"? I have localized Ok and Cancel buttons text using

UIManger.put("Ok","localtext");

Thanks

4 Answers 4

2

You can set the title for all dialogs of JOptionPane provides when you call the show*Dialog method. It is usually the third method parameter. For instance:

JOptionPane.showMessageDialog(Component parentComponent, Object message, String title, int messageType)

Typically one reads the localized string (e.g. dialog title) from a resource bundle and passes it as argument to the show*Dialog call.

1

You won't expect this answer...

Under you JDK installation directory, you will be able to find file named src.zip. Just unpack it and navigate to javax/swing/JOptionPane.java. It contains following method that might answer your question:

public static String showInputDialog(Component parentComponent,
    Object message) throws HeadlessException { 
    return showInputDialog(parentComponent, message, UIManager.getString(
        "OptionPane.inputDialogTitle", parentComponent), QUESTION_MESSAGE);
}

As you can see OptionPane.inputDialogTitle is probably what you were looking for... Although there is easier way to set title. However, if you are tempted to do everything the same way, you might just as well use UIManager.
I am writing this, because you would surely need this method for other dialogs, i.e. JFileChooser. And by doing so, you might find out that Desktop folder name is hard-coded in Java 6 on Windows Vista+ (it is resolved to actual folder name on disk, which is always "Desktop").

1
1

Use java.util.ResourceBundle

With it, you store the localized values under specific keys in a .properties file, and then get them by: ResourceBundle.getBundle("messages", locale).get("messageKey").

.properties files are formed by having basename_locale.properties. For example, messages_en.properties

In regard to JOptionPane, see here how to customize the texts. As for the title - you can pass it as an argument.

6
  • I don't think that was what Feras asked about :) Apr 22, 2011 at 17:31
  • Why do you think so? I believe ResourceBundle is the standard for that, unlike using the UIManager
    – Bozho
    Apr 22, 2011 at 17:34
  • Really I don't mean this I mean localize strings in JDK
    – Feras Odeh
    Apr 22, 2011 at 17:34
  • 2
    To some extent you are right, ResourceBundle is a standard way of externalizing string in Java. However, one need to use UIManager in order to translate Swing Dialogs into his language. This does not necessary mean hard-coding them. Apr 22, 2011 at 17:38
  • @PawełDyda surely you don't need to use UIManager and it may not even be the correct way to do it. I went looking and found that the LAF classes use string resources like anyone else, so surely getting those translated and putting them on the classpath is more sensible and fits with the whole way things are supposed to work better than modifying the LAF.
    – Hakanai
    Mar 29, 2015 at 7:10
0

Localizing JoptionPane Title, Message and options (YES, NO, OK, CANCEL):

Resource bundles allow us to set different languages. Bellow example illustrates how to create Message dialog with Chinese messages and options…

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

public class LocalizeMessagePane {

    private static ResourceBundle resourceBundle = null;    
    //Language Property location
    private static final String PROPERTY_LOCATION = "resource"; 

    public static void main(String[] args) {        

        resourceBundle = getLanguageBundle(); //try to get Chinese bundle       

        // Get Yes/No option text from bundle

        Object[] options = { resourceBundle.getObject("Yes").toString(),
                             resourceBundle.getObject("No").toString() };

        int option = JOptionPane.showOptionDialog(null, resourceBundle
                .getObject("sureDo").toString(),                                
                resourceBundle.getObject("title").toString(),
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                options, options[1]);

        if(option==JOptionPane.YES_OPTION){
            // do stuff
        }       
    }   

    public static ResourceBundle getLanguageBundle() {
        try {
            /* The Property file should be placed under 'resource' directory. */
            resourceBundle = new PropertyResourceBundle(new InputStreamReader(
                    new FileInputStream(PROPERTY_LOCATION
                            + "/Bundle_zh_CN.properties"),
                    Charset.forName("UTF-8")));

        } catch (Exception e) {
            // do stuff
        }
        return resourceBundle;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.