Saj
Saj

Reputation: 317

Change Button text in JOptionPane showConfirmDialog

I have a JPanel within JOptionPane.showConfirmDialog. I would like to change the Yes/No/Cancel buttons to custom values but unsure how to.

    private void displayGUI(String msg) {
    UIManager.put("OptionPane.okButtonText", "SNOOZE");
    final int selectedVal = JOptionPane.showConfirmDialog(null, getPanel(msg));
    if (selectedVal == JOptionPane.OK_OPTION){
        System.out.println("Ok button has been pressed.");
    }
}

private JPanel getPanel(String msg) {
    JPanel panel = new JPanel();
    JLabel label = new JLabel (msg, JLabel.CENTER);
    label.setFont (label.getFont ().deriveFont (32.0f));
    panel.setPreferredSize(new Dimension(500,500));
    panel.add(label);

    return panel;
}

I tried using the UIManager but was unsuccessful.

Upvotes: 4

Views: 12473

Answers (1)

Deximus
Deximus

Reputation: 453

The following code was salvaged from oracle's documentation site. It seems to work just fine.

Object[] options = {"Yes, please",
                "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,     //do not use a custom Icon
options,  //the titles of buttons
options[0]); //default button title

Output

The source can be found here.

Upvotes: 17

Related Questions