Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the OK Cancel string in JOptionPane

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

like image 564
Harts Avatar asked Jan 06 '12 19:01

Harts


People also ask

How do I change the font in JOptionPane?

There is an easy way to change the default font in JOptionPane . Pass a string modified in html format, which means you can either use <font> tag or even CSS. Using <font> tag.

What integer value will be returned if the user will click the Cancel button?

If the user clicks "cancel", the response will be null. If they click "ok" without entering any text the response will be the empty string.

What does JOptionPane Showconfirmdialog return?

It will return an int which represents which button was pressed. Run the code without the method call, and see what order the buttons are in ('Yes No Cancel' or whatever). If you hit yes in that example, it'd return 0, No would return 1, and Cancel 2 (closing the dialog returns -1).

What is return type of JOptionPane showInputDialog?

JOptionPane. showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise.


2 Answers

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");
like image 123
Bhesh Gurung Avatar answered Sep 30 '22 18:09

Bhesh Gurung


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.

like image 37
zeller Avatar answered Sep 30 '22 16:09

zeller