I want to add a color-indicator to my JFrame. It should be turned to be red when I click a button and if I do not click a button it should be green. How should I implement this?
Asked
Active
Viewed 7,257 times
1
-
in JFrame's content, you can add a JLabel, do you want to add at the top of the JFrame, at decoration ? - near the minimize and maximize buttons? – Apr 25 '13 at 12:57
-
do you want it in the header? What do you want as an indicator? be more specific – Oliver Watkins Apr 25 '13 at 13:04
-
I want it to be in any place in the jframe and very small it can be circular. – RSF Apr 25 '13 at 13:14
1 Answers
8
JLabel lblLed = new JLabel("•");
lblLed.setForeground(Color.Green);
Add an ChangeListener to your JButton and in the StateChanged() method add this:
if (buttonIsPressed) {
lblLed.setForeground(Color.Red);
} else {
lblLed.setForeground(Color.Green);
}

DeadlyJesus
- 1,503
- 2
- 12
- 26
-
1+1 for unicode; see also [`ColorIcon`](http://stackoverflow.com/a/3072979/230513). – trashgod Apr 25 '13 at 17:25