Why does setBackground to JButton does not work?
Asked Answered
M

6

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.

Mccormick answered 14/2, 2011 at 10:14 Comment(4)
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() might be what you're after.Ananna
Can you post more details? Your UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); is after or before btn.setBackground(backgroundColor) command?Intelligibility
@Serhiy, my LookAndFeel was before. And I do not want to use it at all.Mccormick
@proactif is correct. This related How to Answer[1] discusses several alternative approaches. [1]:stackoverflow.com/questions/3420431Barthol
F
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);
Facility answered 18/7, 2013 at 12:31 Comment(0)
B
7

add btn.setBorderPainted(false)

Bulgar answered 20/2, 2017 at 19:32 Comment(0)
U
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.

Underplot answered 14/2, 2011 at 11:7 Comment(1)
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.Sipple
P
0
    btn.setBorderPainted(false);
    btn.setOpaque(true);
    btn.setBackground(Color.BLACK);
    btn.setForeground(Color.BLUE);
Periphrasis answered 6/2, 2021 at 22:32 Comment(0)
I
-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);

        }
    });
}

}

Intelligibility answered 14/2, 2011 at 11:12 Comment(0)
I
-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.

Ichnography answered 27/6, 2012 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.