Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I localize the JOptionPane Yes/No/Cancel option?

I have a question regarding the JOptionPane.showConfirmDialog. The buttons I get are Yes, No and Cancel, but I was wondering if it was possible to localize these three buttons to my language? I realize I can just create a confirmation dialogue of my own with my JButtons but I was wondering if this was possible as well.

Thanks in advance.

like image 659
Nikola Luburic Avatar asked Jan 04 '13 13:01

Nikola Luburic


2 Answers

The simple way is to set the JOptionPane with the desired locale, like this:

Locale locale = new Locale("pt","BR");
JOptionPane.setDefaultLocale(locale);

If you have a singleton class that treats all the messages from your ResourceBundle, then you just need to set it once in the locale initialization. For example:

public void setLocale(Locale locale) {
    if (_bundle == null || !locale.equals(_locale)) {
        _locale = locale;
        _bundle = ResourceBundle.getBundle("resources.i18n.MessagesBundle", locale);

        JOptionPane.setDefaultLocale(_locale);

        _logger.info("Got new resource bundle for language-country: " + _locale.getLanguage() + "-" +
                locale.getCountry());
    }
}

Cheers!

like image 71
jfajunior Avatar answered Sep 30 '22 02:09

jfajunior


This is enough:

UIManager.put("OptionPane.yesButtonText", "Ya");

etc

like image 31
BZM Avatar answered Sep 30 '22 04:09

BZM