0

Does anybody know how to set the background color temporarily on a Button.

buttons[randomI][randomJ].setBackgroundColor(Color.rgb(155, 17, 30));

Here is what I have but I only want to set the background color of this for a certain period of time. I understand that a way to go about this is to remove the background color after a certain period of time but I don't know how to remove background color. I referenced: How to get JButton default background color? and the solutions did not work for me.

In the top answer:

btn.setBackground(new JButton().getBackground());

JButton does not exist for me and using new Button().getBackground says that it can not resolve constructor. So is there any way to temporarily set the background color?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Louis Ye
  • 134
  • 1
  • 13

1 Answers1

0

Store the previous color of the Button:

int color = 0; // Black default
Drawable drawable = buttons[randomI][randomJ].getBackground();
if (drawable instanceof ColorDrawable) {
    color = ((ColorDrawable) drawable).getColor();
}

Set the new color temporarily:

buttons[randomI][randomJ].setBackgroundColor(Color.rgb(155, 17, 30));

and after some period restore the initial color:

buttons[randomI][randomJ].setBackgroundColor(color);
forpas
  • 160,666
  • 10
  • 38
  • 76