7

How can I set text background color in JOptionPane?

Image:

enter image description here

UIManager UI = new UIManager();
UI.put("OptionPane.background", Color.white);
UIManager.put("Button.background", Color.white);
UI.put("Panel.background", Color.white);
UI.put("OptionPane.foreground", Color.white);
UI.put("OptionPane.messagebackground", Color.white);
UI.put("OptionPane.textbackground", Color.white);
UI.put("OptionPane.warningDialog.titlePane.shadow", Color.white);
UI.put("OptionPane.warningDialog.border.background", Color.white);
UI.put("OptionPane.warningDialog.titlePane.background", Color.white);
UI.put("OptionPane.warningDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.questionDialog.border.background", Color.white);
UI.put("OptionPane.questionDialog.titlePane.background", Color.white);
UI.put("OptionPane.questionDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.questionDialog.titlePane.shadow", Color.white);
UI.put("OptionPane.messageForeground", Color.white);
UI.put("OptionPane.foreground", Color.white);
UI.put("OptionPane.errorDialog.border.background", Color.white);
UI.put("OptionPane.errorDialog.titlePane.background", Color.white);
UI.put("OptionPane.errorDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.errorDialog.titlePane.shadow", Color.white);

JOptionPane.showMessageDialog(null, "Hello world", "HELLO WORLD", JOptionPane.INFORMATION_MESSAGE);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Faken143
  • 209
  • 1
  • 4
  • 10

3 Answers3

9

Why not you simply add a custom JPanel at this place as shown below :

JOptionPane.showMessageDialog(frame, getLabelPanel(), "Hello World!",
                                        JOptionPane.INFORMATION_MESSAGE);

You can get the JPanel from a method, like :

private JPanel getLabelPanel() {
    JPanel panel = new JPanel();
    panel.setOpaque(true);
    panel.setBackground(Color.BLUE);
    JLabel helloLabel = new JLabel("Hello World!", JLabel.CENTER);
    helloLabel.setForeground(Color.WHITE);
    panel.add(helloLabel);

    return panel;
}

OUTPUT :

PANEIMAGE


UPDATE 1 :

Else you can try this to change everything,

uimanager.put("OptionPane.background", Color.BLUE);
uimanager.put("OptionPane.messagebackground", Color.BLUE);
uimanager.put("Panel.background", Color.BLUE);

UPDATED OUTPUT :

OPTIONPANEIMAGE

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • @user2464161 : If this is not what you wanted, why not use a `JDialog`, that way you don't really need to play with the `UIManager` and work out a solution without any fuss. – nIcE cOw Jul 29 '13 at 10:00
  • 1
    I believe the question explicitly asks for a way to change the background color of a JOptionPane not a way around it. – 137 Jul 29 '13 at 10:07
  • 1
    @DustinE. : Its just an alternative I suggested. Sometimes what one is thinking, might not be the right track. So its about ideas that one gets from others, that the same thingy can be achieved through other means too :-) Though taken your advice, and updated likewise :-) – nIcE cOw Jul 29 '13 at 10:12
  • @mKorbel : Ahha, I just got, you were referring to the OP's side `LookAndFeel`. – nIcE cOw Jul 29 '13 at 14:48
2

try

UIManager UI=new UIManager();
 UI.put("OptionPane.background",new ColorUIResource(255,255,0));
 UI.put("Panel.background",new ColorUIResource(255,255,0));
Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
2

The cleanest would be using JDialog to create your own JOptionPane replacement, like nIcE cOw suggested. JOptionPane packs a lot of junk in its frame, and several of the components check no JOptionPane specific properties at all.

If you insist on using JOptionPane anyway, and want to change the background for only those, not for everything in the application, I'd say that if you want to set the background for the entire dialog with all its subcomponents, your best bet is to iterate all the window contents and call setBackground() on each. That could be done in its UI class, or individually for dialogs obtained with JOptionPane.createDialog(). Roughly like this:

void colorComponentTree(Component component, Color color) {
    if (component instanceof Container) {
        if (component instanceof JComponent) {
             ((JComponent) component).setBackground(color);
        }

        for (Component child : ((Container) component).getComponents()) {
             colorComponentTree(child, color);
        }
    }
}

That is however really ugly and I seriously recommend taking the custom dialog route.

kiheru
  • 6,588
  • 25
  • 31