1

I am not getting a complete background color for JOptionPane.

Below there is one of my output screen:

https://i.stack.imgur.com/i4tvh.png

And my code:

JFrame frame1 = new JFrame("Showing Error Message");
                    UIManager UI=new UIManager();
                    UI.put("OptionPane.background", Color.BLUE);
                    UI.put("OptionPane.messagebackground", Color.BLUE);
                    UI.put("Panel.background", Color.BLUE);
                    JOptionPane.showMessageDialog(frame1,errorMessage1);

This is my Output Image

enter image description here

honey1
  • 75
  • 1
  • 14

2 Answers2

3

First of all, the public methods of UIManager are static. It is incorrect, misleading, and pointless to create an instance of UIManager. The correct way to invoke those methods is:

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

This is the whole sample.

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Main extends JFrame
{
   public static void main(String []args) {
      UIManager.put("OptionPane.background", Color.blue);
      UIManager.put("Panel.background", Color.blue);
      UIManager.put("Button.background", Color.white);

      String value = JOptionPane.showInputDialog("Enter your name");
      System.out.println("Hello " + value);

      // exit awt thread
      System.exit(1);
   }
}

screenshot

ziLk
  • 3,120
  • 21
  • 45
0

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

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.

DSF.Inc
  • 3,021
  • 2
  • 6
  • 9