How to change background color of JOptionPane?
Asked Answered
F

6

14

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) {                  }`
Fairchild answered 30/1, 2012 at 13:54 Comment(0)
I
17

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);
Interface answered 30/1, 2012 at 13:58 Comment(2)
Note that this will change the background for all instances.Ethiopian
Is it possible to add my custom button to it also?Fairchild
H
5

JOptionPane image

For anyone having the same problem like above image, I found/adapted a solution. On my system, I got that result, whether I used the UIManager solution as others have posted, or made a JDialog and used jd.getContentPane().setBackground(Color.white). So here is the work-around I came up with, where you loop recursively through each component in the JOptionPane, and set each JPanel's background color:

private void getComponents(Container c){

    Component[] m = c.getComponents();

    for(int i = 0; i < m.length; i++){

        if(m[i].getClass().getName() == "javax.swing.JPanel")
            m[i].setBackground(Color.white);

        if(c.getClass().isInstance(m[i]))
            getComponents((Container)m[i]);
    }
}

In your code where you want to have the message pop-up, something along the lines of:

pane = new JOptionPane("Your message here", 
                JOptionPane.PLAIN_MESSAGE ,JOptionPane.DEFAULT_OPTION);
        getComponents(pane);
        pane.setBackground(Color.white);
        jd = pane.createDialog(this, "Message");
        jd.setVisible(true);

Where JOptionPane pane and JDialog jd have previously been created. Hope this helps anyone who had that issue.

Hoch answered 13/6, 2016 at 21:56 Comment(1)
This is the best responseDisciplinarian
S
3

Use this code if you have the same problem as erik k atwood. This solves the problem:

UIManager.put("OptionPane.background", Color.WHITE);
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.WHITE);
Secondrate answered 13/12, 2016 at 10:14 Comment(0)
S
2

Use something like this to change the background color just for this one message display and not the whole system...

    Object paneBG = UIManager.get("OptionPane.background");
    Object panelBG = UIManager.get("Panel.background");
    UIManager.put("OptionPane.background", new Color(...));
    UIManager.put("Panel.background", new Color(...));

    int ret = messageBox(msg, null, (short)type);

    UIManager.put("OptionPane.background", paneBG);
    UIManager.put("Panel.background", panelBG);
Shelley answered 11/5, 2015 at 13:33 Comment(1)
What is the method "messageBox()"?Crosby
K
1

UIManager.put("OptionPane.background", Color.WHITE); UIManager.getLookAndFeelDefaults().put("Panel.background", Color.WHITE);

Put this code before you call your JOptionPane dialog like:

UIManager.put("OptionPane.background", Color.decode("#3c6562"));
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.decode("#3c6562"));
    
int input= JOptionPane.showConfirmDialog
(
   null,
   "Close the programm?",
   "Exit",
   JOptionPane.YES_NO_OPTION
 );

 if(input == 0)
 {
    System.exit(0);
 }
Kaycekaycee answered 15/9, 2020 at 20:25 Comment(0)
C
1

In nimbus look and feel these all codes are not usable.

So solution is,

UIManager.put("control", new Color(0, 0, 0));

This is also call "Dark Nimbus" add this top of your main frame's main method. So it will automatically change All JOptionPane's background.

And also you can't change button background with

UIManager.put("OptionPane.buttonBackground", BLACK);

So you should use,

UIManager.put("nimbusBase", new Color(0, 0, 0));

Remember - but unfortunately, this code will change all your buttons etcs' background. So you have to add *.setBackground(...); to all other objects.

If you want to change foreground of JOptionPane you should use

UIManager.put("text", new Color(255, 255, 255));

Again unfortunately this will change your all of text's foreground.

These all codes are called Dark Nimbus.

If you're using nimbus you can try these UIManager codes to customize nimbus look and feel.

UIManager.put("control", new Color(0, 0, 0));
UIManager.put("info", new Color(0, 0, 0));
UIManager.put("nimbusBase", new Color(0, 0, 0));
UIManager.put("nimbusAlertYellow", new Color(248, 187, 0));
UIManager.put("nimbusDisabledText", new Color(255, 255, 255));
UIManager.put("nimbusFocus", new Color(115, 164, 209));
UIManager.put("nimbusGreen", new Color(176, 179, 50));
UIManager.put("nimbusInfoBlue", new Color(66, 139, 221));
UIManager.put("nimbusLightBackground", new Color(0, 0, 0));
UIManager.put("nimbusOrange", new Color(191, 98, 4));
UIManager.put("nimbusRed", new Color(169, 46, 34));
UIManager.put("nimbusSelectedText", new Color(255, 255, 255));
UIManager.put("nimbusSelectionBackground", new Color(18, 134, 175));
UIManager.put("text", new Color(255, 255, 255));

You can try these codes. In my project the nimbus seem as

enter image description here

enter image description here

enter image description here

But I always recommend using "Flatleaf" (Search google "FlatLafLookAndFeel" or go to jar.download.com"). It is a professional and you can change all to your own.

Candlewick answered 13/2, 2021 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.