Feras Odeh
Feras Odeh

Reputation: 9296

How to localize JOptionPane Message title?

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

Upvotes: 1

Views: 2179

Answers (4)

Rajeshkumar Natarajen
Rajeshkumar Natarajen

Reputation: 41

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;
    }

Upvotes: 0

Bozho
Bozho

Reputation: 597106

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.

Upvotes: 1

Paweł Dyda
Paweł Dyda

Reputation: 18662

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").

Upvotes: 1

Stefan Endrullis
Stefan Endrullis

Reputation: 4208

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.

Upvotes: 2

Related Questions