Search code examples
javaswingjoptionpanenimbusuimanager

JOptionPane button size (Nimbus LAF)


Recently I've been working on a Swing project with Nimbus Look and Feel. I want to set all buttons in a JOptionPane to have the same size, but in vain.

import javax.swing.*;

public class NimbusTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {    
                try {
                    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if (("Nimbus").equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                UIManager.put("OptionPane.sameSizeButtons", true);    
                String[] options = new String[]{"--------------------","short","1"};
                int option = JOptionPane.showOptionDialog(null, "Nimbus problem", "JOptionPane", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);    
            }
        });
    }
}

What I want is a JOptionPane with three buttons having the same size. However, I got the following result:

enter image description here

My code UIManager.put("OptionPane.sameSizeButtons", true); seems to be ignored. What should I do to create a JOptionPane with same size buttons if I don't want to recreate a JOptionPane-like dialog?


Solution

  • Replace

     UIManager.put("OptionPane.sameSizeButtons", true);
    

    with

     UIManager.getLookAndFeelDefaults().put("OptionPane.sameSizeButtons", true);
    

    And it works like a charm

    enter image description here