14

I want to set the text of OK and CANCEL buttons in JOptionPane.showInputDialog to my own strings.

There is a way to change the buttons' text in JOptionPane.showOptionDialog, but I couldn't find a way to change it in showInputDialog.

Kara
  • 6,115
  • 16
  • 50
  • 57
Daniel Briskman
  • 399
  • 2
  • 3
  • 11

4 Answers4

20

if you don't want it for just a single inputDialog, add these lines prior to creating dialog

UIManager.put("OptionPane.cancelButtonText", "nope");
UIManager.put("OptionPane.okButtonText", "yup");

where 'yup' and 'nope' is the text you want displayed

Michael Dunn
  • 818
  • 5
  • 3
  • And if you do happen to only want to use it for a single dialog, you can always change it back immediately afterwards. – hjk321 Aug 14 '20 at 04:06
14

The code below should make a dialog appear and you can specify the button text in the Object[].

Object[] choices = {"One", "Two"};
Object defaultChoice = choices[0];
JOptionPane.showOptionDialog(this,
             "Select one of the values",
             "Title message",
             JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE,
             null,
             choices,
             defaultChoice);

Also, make sure to look through the Java tutorials on the Oracle site. I found the solution at this link in the tutorials http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#create

Gregory Peck
  • 636
  • 7
  • 22
9

If you want the JOptionPane.showInputDialog with custom button texts, you could extend JOptionPane:

public class JEnhancedOptionPane extends JOptionPane {
    public static String showInputDialog(final Object message, final Object[] options)
            throws HeadlessException {
        final JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE,
                                                 OK_CANCEL_OPTION, null,
                                                 options, null);
        pane.setWantsInput(true);
        pane.setComponentOrientation((getRootFrame()).getComponentOrientation());
        pane.setMessageType(QUESTION_MESSAGE);
        pane.selectInitialValue();
        final String title = UIManager.getString("OptionPane.inputDialogTitle", null);
        final JDialog dialog = pane.createDialog(null, title);
        dialog.setVisible(true);
        dialog.dispose();
        final Object value = pane.getInputValue();
        return (value == UNINITIALIZED_VALUE) ? null : (String) value;
    }
}

You could call it like this:

JEnhancedOptionPane.showInputDialog("Number:", new Object[]{"Yes", "No"});
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
4

Please see How to Make Dialogs: Customizing Button Text.

mre
  • 43,520
  • 33
  • 120
  • 170