2

I am making a GUI interface and I am trying to change the background and foreground color of my windows with the following code:

  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Container;

  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;

  public class Test
  {

public static void changeColor(String typeOfColor, Component component, Color color)
{
    if (typeOfColor.equals("Background"))
    {
        component.setBackground(color);
    }
    else if (typeOfColor.equals("Foreground"))
    {
        component.setForeground(color);
    }

    if (component instanceof Container)
    {
        for (Component child : ((Container) component).getComponents())
        {
            changeColor(typeOfColor, child, color);
        }
    }
}

public static void main(String[] args)
{
    JPanel panel = new JPanel();
    JButton cancelButton = new JButton("Cancel");
    panel.add(cancelButton);

    changeColor("Background", panel, new Color(0, 255, 0));

    JFrame frame = new JFrame("Frame");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.setVisible(true);
    frame.pack();
}
}

However, no matter what is the color I choose, the buttons still display the background color as grey. How do I change the background color properly? I have looked around and most answers mention the setBackground method, but that does not work for me.

Thanks in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Benoit Goderre
  • 527
  • 2
  • 9
  • 25
  • 3
    Could you post a [Runnable example](http://stackoverflow.com/help/mcve) please? Not your full code, but an easy example with the minimum and relevant one that we can copy-paste and see the same issue as you? – Frakcool Dec 18 '15 at 18:46
  • What LookAndFeel is your application using? – Nicholas Hirras Dec 18 '15 at 18:55
  • Thank you for the answers. I have updated my code, which does work, strangely in the short example, but not in my app, which contains way too many controls to display here. Also, I do not set a specific LookAndFeel. – Benoit Goderre Dec 18 '15 at 19:05
  • 2
    Your title says you want to change the background of a JButton, but your example is changing the color of a JPanel. It could be the background color for a JButton in your specific LookAndFeel can't be overridden. – Nicholas Hirras Dec 18 '15 at 19:11
  • Oh, good point, How do I know which LookAndFeel could allow me to override these settings? – Benoit Goderre Dec 18 '15 at 19:14
  • 2
    You might want to try button.setOpaque(false); to allow the background color to show through. – cliff2310 Dec 18 '15 at 19:17
  • Thank you to all for the help! Nicholas, I was actually setting the LookAndFeel in one of my subpanels. Thank you very much! – Benoit Goderre Dec 18 '15 at 19:52
  • Nicholas, forgot to mention that if you set it as an answer, I will accept it ;). – Benoit Goderre Dec 18 '15 at 20:07

3 Answers3

4

Check on your current program the order of these lines:

panel.add(cancelButton);
changeColor("Background", panel, new Color(0, 255, 0));

If you have them in this order you get this output:

enter image description here

But if you change the order:

changeColor("Background", panel, new Color(0, 255, 0));
panel.add(cancelButton);

You get this:

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1
    I'm glad it helped, if so please [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). In your case, you want your panels in different color? If so also check [`JPanel#setOpaque`](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setOpaque(boolean)) it might be of some help, and please don't call me sir, makes me feel old haha – Frakcool Dec 18 '15 at 19:27
  • 1
    (Deleted previous comment as I couldn't edit it again and it was all broken). Thank you Frakcool! – Benoit Goderre Dec 18 '15 at 19:30
  • I realize my question is not quite clear. I have a panel that contains many sub panels. I add all of the sub panels to the main panel, then change the color of the main panel. (I need to change it after all the components are added and that is why there is the recursion). When I do that, the buttons background color remains Grey. So, I do not have the luxury of changing the order of the changeColor method, – Benoit Goderre Dec 18 '15 at 19:34
  • Hmm then unaccept the answer so more people can see your question and try to get the same error on your sample code – Frakcool Dec 18 '15 at 19:36
  • Or I could add a different and more precise question :P. – Benoit Goderre Dec 18 '15 at 19:37
  • Go ahead! n.n But remember placing your code so we can try helping – Frakcool Dec 18 '15 at 19:38
  • Found that the source of the issue was the look and feel which was mentioned by someone else. Thanks again for your help and have a great day! – Benoit Goderre Dec 18 '15 at 20:07
  • @BenoitGoderre oh then you should accept their answer :) I'm glad you got it solved – Frakcool Dec 18 '15 at 20:18
1

Nicholas Smith solved my issue.

In the comments, he mentioned "It could be the background color for a JButton in your specific LookAndFeel can't be overridden."

I was setting the look and feel in my code and once I removed that part of the code, my buttons' background color was changed successfully.

Thanks to you!

Benoit Goderre
  • 527
  • 2
  • 9
  • 25
0

You should change the LookAndFeel of your GUI with UIManager.setLookAndFeel().

It happened to me several times in MacOSX