Change the OK Cancel string in JOptionPane

2020-04-03 09:09发布

I was wondering is it possible to change the OK Cancel Button to custom string in java? I have

JOptionPane.showConfirmDialog(message, title, JOptionPane.OK_CANCEL_OPTION);

Right now, the button will show "OK" and "Cancel". Is it possible to change the text for that? for example into "A" and "B" or maybe japanese text?

Thank you

3条回答
疯言疯语
2楼-- · 2020-04-03 09:34

Look at the javadocs in the detailed class description part:
You aren't limited to this set of option buttons. You can provide any buttons you want using the options parameter.
What (options) is also described there. Anyway the default texts (i.e. OK/Cancel) are usually based on the computer locale, but for custom labels use the method described in the javadocs.

查看更多
▲ chillily
3楼-- · 2020-04-03 09:39

Looks like instead of JOptionPane.showConfirmDialog you are going to have to use JOptionPane.showOptionDialog, which lets you supply your own texts as an array.

Try the following:

JOptionPane.showOptionDialog(null, 
        "Do you like this answer?", 
        "Feedback", 
        JOptionPane.OK_CANCEL_OPTION, 
        JOptionPane.INFORMATION_MESSAGE, 
        null, 
        new String[]{"Yes I do", "No I don't"}, // this is the array
        "default");
查看更多
放我归山
4楼-- · 2020-04-03 09:51

I know this is very old question. But for future reference, we can do it by add 2 lines before showConfirmDialog:

UIManager.put("OptionPane.cancelButtonText", "Go back");
UIManager.put("OptionPane.okButtonText", "Save");
查看更多
登录 后发表回答