0

I am trying to create a grid with randomly colored buttons. I was able to create the grid, add buttons to it and even create a random color- only issue is the color is only showing up on the edges of the buttons. I even tried using the button.setForeground(color) as a possible solution but it did not work. I want the entire button to be colored with my randomly generated color. How can I do so?enter image description here

for (int i=0; i < 4; i++){
        for (int j=0; j < 3; j++){
            Color color = randomColorGenerator();
            buttons[i][j] = new JButton();
            buttons[i][j].setBackground(color);
            buttons[i][j].setOpaque(true);
            jp.add(buttons[i][j]);
        }
    }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Chandrika
  • 41
  • 4
  • 1
    Try also using `setContentAreaFilled` and `setBorderPainted` (and probably `setFocusPainted`) passing them `false` – MadProgrammer Feb 25 '16 at 12:44
  • The answer referred to explains how to add an image to a JButton with a transparent background. – The F Feb 25 '16 at 12:51

1 Answers1

0
buttons[i][j].setBackground(color);
buttons[i][j].setContentAreaFilled(false);
buttons[i][j].setOpaque(true);

Should do the trick. Also check this answer.
If you want to remove the border color, add this line:

buttons[i][j].setBorder(null);
Community
  • 1
  • 1
The F
  • 3,647
  • 1
  • 20
  • 28