Emre
Emre

Reputation: 79

How to change the default text of buttons in JOptionPane.showMessageDialog

I am using showMessageDialog in my program. I want to change the text of "OK" button in code. However, I couldn't find how I can do it.

Note: There is language option in my program, when user change language I need to change the texts.

Thank you for your help

Upvotes: 0

Views: 1738

Answers (2)

Nepal12
Nepal12

Reputation: 593

Or you can use like this,

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;
}
}

and call it like JEnhancedOptionPane.showInputDialog("Number:", new Object[]{"Yes", "No"}); //here you can change to any text

Actually this is taken from here http://stackoverflow.com/questions/14407804/how-to-change-the-default-text-of-buttons-in-joptionpane-showinputdialog

Upvotes: 0

Paco Abato
Paco Abato

Reputation: 4065

This will change the texts for all the message dialogs:

UIManager.put("OptionPane.cancelButtonText", cancelText);
UIManager.put("OptionPane.okButtonText", okText);

Upvotes: 1

Related Questions