Search code examples
javaswingbackground-colorjoptionpane

How to change background color of JOptionPane?


I have added JOptionPane to my application but I do not know how to change background color to white?

`int option = JOptionPane.showConfirmDialog(bcfiDownloadPanel,
            new Object[]{"Host: " + source, panel},
            "Authorization Required",
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE
    );

    if (option == JOptionPane.OK_OPTION) {                  }`

Solution

  • By using the UIManager class

     import javax.swing.UIManager;
    
     UIManager UI=new UIManager();
     UI.put("OptionPane.background",new ColorUIResource(255,0,0));
     UI.put("Panel.background",new ColorUIResource(255,0,0));
    

    or

     UIManager UI=new UIManager();
     UI.put("OptionPane.background", Color.white);
     UI.put("Panel.background", Color.white);
    
     JOptionPane.showMessageDialog(null,"Text","SetColor",JOptionPane.INFORMATION_MESSAGE);