20

Below is the code which creates 9 buttons in gridlayout form on a specific pannel3. What i want is to make the background of each button black with grey text over it. Can anyone help please?

 for(int i=1;i<=9;i++)
 {
     p3.add(new JButton(""+i));
 }
Jonik
  • 80,077
  • 70
  • 264
  • 372
Salar
  • 259
  • 2
  • 6
  • 16

8 Answers8

28

Check out JButton documentation. Take special attention to setBackground and setForeground methods inherited from JComponent.

Something like:

for(int i=1;i<=9;i++)
{
    JButton btn = new JButton(String.valueOf(i));
    btn.setBackground(Color.BLACK);
    btn.setForeground(Color.GRAY);
    p3.add(btn);
}
Suma
  • 33,181
  • 16
  • 123
  • 191
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 17
    This does not work with Java 8 on the Mac. The background color is the color behind the button. The button is then drawn on top of the black background and the button is gray with gray text. (you cannot read the text). In fact, it's worse... the black background isn't drawn at all unless `setOpaque(true)` is called first. – Jason Mar 10 '16 at 23:25
  • 4
    Doesn't work with Java 8. as @Jason said. The background color is the color behing the button. – William May 11 '18 at 20:52
  • 3
    Using `setBorderPainted(false)` would solve the above mentioned issue. – Homaei Jun 08 '20 at 05:17
12

Simple:

btn.setBackground(Color.red);

To use RGB values:

btn[i].setBackground(Color.RGBtoHSB(int, int, int, float[]));

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Ali Mohammadi
  • 1,306
  • 1
  • 14
  • 28
3

Changing the background property might not be enough as the component won't look like a button anymore. You might need to re-implement the paint method as in here to get a better result:

enter image description here

Community
  • 1
  • 1
luca
  • 7,178
  • 7
  • 41
  • 55
2
for(int i=1;i<=9;i++) {
    p3.add(new JButton(""+i) {{
        // initialize the JButton directly
        setBackground(Color.BLACK);
        setForeground(Color.GRAY);
    }});
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
2

You may or may not have to use setOpaque method to ensure that the colors show up by passing true to the method.

Tanner
  • 1,214
  • 3
  • 12
  • 16
2

It seems that the setBackground() method doesn't work well on some platforms (I'm using Windows 7). I found this answer to this question helpful. However, I didn't entirely use it to solve my problem. Instead, I decided it'd be much easier and almost as aesthetic to color a panel next to the button.

Community
  • 1
  • 1
GregNash
  • 1,316
  • 1
  • 16
  • 25
1

Use the setBackground method to set the background and setForeground to change the colour of your text. Note however, that putting grey text over a black background might make your text a bit tough to read.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

I tried the previous solutions but still couldn't change the color. Came across another article and solved my problem. The button is made of different layers. Removing all of them helps:

    btn.setOpaque(true);
    btn.setContentAreaFilled(true);
    btn.setBorderPainted(false);
    btn.setFocusPainted(false);
    btn.setBackground(Color.GRAY); // for the background
    btn.setForeground(Color.white); // for the text
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ash
  • 1