11

I have the following simple code:

btn = new JButton();
btn.setBackground(backgroundColor)

I worked when I used:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");

But it stopped to work after I have commented the above line. Does anybody know why it can happen and how I can set a background color to a button without the usage of an explicit Look and Feel?

ADDED

It seems to me that I need to use getBackground. But I do not know how.

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 8
    On an unrelated note: please don't use that `UIManager` line like that: hardcoding the PLAF class name means your code will not run when that PLAF is not available (for example on non-Windows JDKs **or** on newer Windows JDK that might stop shipping that specific class). [`UIManager.getSystemLookAndFeelClassName()`](http://download.oracle.com/javase/6/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName()) might be what you're after. – Joachim Sauer Feb 14 '11 at 10:20
  • Can you post more details? Your UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); is after or before btn.setBackground(backgroundColor) command? – Serhiy Feb 14 '11 at 10:38
  • @Serhiy, my LookAndFeel was before. And I do not want to use it at all. – Roman Feb 14 '11 at 10:53
  • @proactif is correct. This related [answer][1] discusses several alternative approaches. [1]:http://stackoverflow.com/questions/3420431 – trashgod Feb 14 '11 at 15:32

6 Answers6

14

it is necessary to set Opaque of the element to true for color to be filled

     btn = new JButton();
     btn.setOpaque(true);
     btn.setBackground(backgroundColor);
CoderCoder
  • 358
  • 2
  • 7
7

add btn.setBorderPainted(false)

JaNL
  • 99
  • 1
  • 5
6

From setBackground() javadoc:

It is up to the look and feel to honor this property, some may choose to ignore it.

Maybe your LAF just ignored it.

proactif
  • 11,331
  • 1
  • 17
  • 11
  • While it is possible that it just ignored it, it is highly unlikely. First check the setOpaque option, as that is the more likely cause. – Reid Jan 13 '15 at 19:27
0
    btn.setBorderPainted(false);
    btn.setOpaque(true);
    btn.setBackground(Color.BLACK);
    btn.setForeground(Color.BLUE);
Jin Lim
  • 1,759
  • 20
  • 24
-1

Simply click once on the button you want to set background for, and then go to the properties window. Over there, the second option will be background. Click on its ellipsis, and you can change the color to your liking. The color won't be displayed on the button in your frame until after you run the program. You can see that the button is in the color you preferred.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Dora
  • 7
  • 1
-1

Problem also can be with the way you are creating the button. Check if the code above:

public class Test extends JApplet{

public void init() 
{  
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        public void run() 
        {   
            setSize(200, 200);
            setLayout(null);

            JPanel p = new JPanel();
            getContentPane().add(p);
            p.setSize(getSize());
            p.setLayout(null);

            JButton b = new JButton("test");
            p.add(b);
            b.setBounds(10, 10, 100, 20);
            b.setBackground(Color.GREEN);

        }
    });
}

}

Serhiy
  • 4,073
  • 3
  • 36
  • 66