-2

How can I customize JOptionPane.showInputDialog? For example, I would like to change the Yes and Cancel option to A and B. Remember that it is only for showInputDialog

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RIO
  • 1
  • 2
  • 1
    Read the Swing tutorial on [How to Make Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) for examples of how to customize the button text. – camickr May 20 '21 at 14:44

1 Answers1

0

Have a look at this answer https://stackoverflow.com/a/14408269/15935039 In your case you could do something like this:

public static void main(String[] args) {
    Object[] choices = {"A", "B"};
    Object defaultChoice = choices[0];
    JOptionPane.showOptionDialog(null,
        "Select one of the values",
        "Title message",
        JOptionPane.YES_NO_OPTION,
        JOptionPane.QUESTION_MESSAGE,
        null,
        choices,
        defaultChoice);
}

if you must use showInputDialog then you can do the following:

public static void main(String[] args) {
    UIManager.put("OptionPane.okButtonText", "A");
    UIManager.put("OptionPane.cancelButtonText", "B");

    JOptionPane.showInputDialog("My dialog");      
    
    UIManager.put("OptionPane.cancelButtonText", "Cancel");
    UIManager.put("OptionPane.okButtonText", "OK");
}

Note that using UIManager will change the value for all inputs, so changing it back after your dialog is important.

Asis
  • 319
  • 1
  • 2
  • 14