0

I'm trying to change background color of my JOptionPane through UIManager constants, the backgrounds of the text area and the button area are not affected.

I have posted my code :3 enter image description here

Image

How can I modify their backgrounds too?

public LoginFINAL() {
    this.setImagen("/imagenes/FondoLogin.png");
    UIManager.put("OptionPane.background",Color.RED );  
    UIManager.put("Panel.background",Color.RED);  
    UIManager.put("Button.background",Color.RED); 
    initComponents();
    jLabel3.setVisible(false);
    setLocationRelativeTo(null);
    textUsers.requestFocus();


}

And this is how i call the JoptionPane

else{
          JOptionPane.showMessageDialog(rootPane, "pls");

    }

Is it better now?

  • The background of the Message and the button should be blue – user3579230 Apr 27 '14 at 21:06
  • is the picture your code? – Kick Buttowski Apr 27 '14 at 21:07
  • Yeah it is... after that im calling JoptionPane to show this message... If you need more code, just tell me what you want to see – user3579230 Apr 27 '14 at 21:09
  • Same result... mmm i took a photo of the part where the look and feel is set to the main class... Does it help? http://imgur.com/TQ1Cs7V – user3579230 Apr 27 '14 at 21:21
  • 1
    I still do not know how you got this pic because I did it in my pc and everything turns to blue. could you share all your code plz? – Kick Buttowski Apr 27 '14 at 21:33
  • As @KickButtowski points out, he's getting a different result than you. Please include an [MCVE](http://stackoverflow.com/help/mcve) to allow us to recreate your problem exactly as you have encountered it. – FThompson Apr 27 '14 at 21:37
  • i do not understand it. can you please clarify? – Kick Buttowski Apr 27 '14 at 21:42
  • @user3579230 An [MCVE](http://stackoverflow.com/help/mcve) should be very short, simple, and self-contained. The code you've included is far too large for anyone to realistically want to understand it and help. – FThompson Apr 27 '14 at 21:54
  • I don't know so i think that the problem is when the LookandFell starts... because if i comment the part where the look and feel set the parameters... it turns totally blue,.... Any idea? – user3579230 Apr 27 '14 at 22:14
  • Great question! I think somebody answered it here: http://stackoverflow.com/questions/9064943/how-to-change-background-color-of-joptionpane (Note to people who just say "Possible duplicate"- that doesn't make the asker feel welcome. Be more considerate in the future). – RobotKarel314 Jun 13 '16 at 22:51
  • Possible duplicate of [How to change background color of JOptionPane?](https://stackoverflow.com/questions/9064943/how-to-change-background-color-of-joptionpane) – Joshua Goldberg Jan 25 '19 at 15:26

1 Answers1

0

I had the same problem, here is my solution:

JOptionPane image

For anyone having the problem in the 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.

  • How do we change the background color of the message? It did change the pane and panel. – MertG Apr 10 '18 at 10:40