Changing JButton background colour temporarily?

You can just use the setBackground(...) method to set the Color and then use a Swing Timer to reset the background when it fires Edit: If your problem is that the setBackground() method doesn't work on some LAF's, then you could add an Icon to the button that is simply a solid Color. Then to change the background color you simple change the Icon.

You can just use the setBackground(...) method to set the Color and then use a Swing Timer to reset the background when it fires. Edit: If your problem is that the setBackground() method doesn't work on some LAF's, then you could add an Icon to the button that is simply a solid Color. Then to change the background color you simple change the Icon.

Yep, com.apple.laf. AquaButtonUI is recalcitrant. I sometimes implement Icon and do setIcon(this), drawing in paintIcon() instead of `paintComponent().

– trashgod Mar 18 '10 at 3:41.

One approach is to extend JToggleButton and override paintComponent() to display the color. A javax.swing. Timer can control timing.

Here is a somewhat more elaborate example. Private static class SimonButton extends JToggleButton { private final Color color; Dimension size = new Dimension(100, 100); public SimonButton(Color color) { this. Color = color; } @Override protected void paintComponent(Graphics g) { super.

PaintComponent(g); if (this.isSelected()) { g. SetColor(color); } else { g. SetColor(Color.

LightGray); } g. FillRect(0, 0, this.getWidth(), this.getHeight()); } @Override public Dimension getPreferredSize() { return size; } }.

For your purposes, you don't necessarily have to use a JButton. You can use JLabels or JPanels. During initialization, you can setBackground() on each one to set its color, and add a MouseListener to each one to detect a click.To flash the Simon pattern, create a javax.swing.

Timer that fires once every second. (You may want to make the delay configurable if 1 second seems like a long time.) For simplicity, the timer could setOpaque(false) on all the JLabels, then setOpaque(true) on the JLabel whose color you want to flash. Note that you might want to wait until the next timer iteration before doing setOpaque(true), so the flashes don't run together if you want to flash the same JLabel multiple times in a row.

The advantage of using setOpaque() is that you can set the MouseListener to just call setOpaque(true) on press and setOpaque(false) on release, and check whether the correct JLabel was clicked, without having to repeatedly recompute which color should be used for a given JLabel.

1 This is the right way to go but you should use javax.swing. Timer rather than TimerTask as the setOpaque call should be made on the EDT. – Russ Hayward Mar 17 '10 at 22:14 Thanks for catching that, Russ; I fixed it.

– rob Mar 17 '10 at 23:32.

For your purposes, you don't necessarily have to use a JButton. You can use JLabels or JPanels. During initialization, you can setBackground() on each one to set its color, and add a MouseListener to each one to detect a click.

To flash the Simon pattern, create a javax.swing. Timer that fires once every second.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions