how to change the default text of buttons in JOptionPane.showInputDialog

*爱你&永不变心* 提交于 2019-12-10 12:37:27

问题


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.


回答1:


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



回答2:


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




回答3:


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




回答4:


Please see How to Make Dialogs: Customizing Button Text.




回答5:


Searching for "custom text JOptionPane" in google revealed this answer https://stackoverflow.com/a/8763349/975959



来源:https://stackoverflow.com/questions/14407804/how-to-change-the-default-text-of-buttons-in-joptionpane-showinputdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!