How can I programmatically fire the change listener of a JSlider
?
Asked
Active
Viewed 2,486 times
1

mKorbel
- 109,525
- 20
- 134
- 319

Rendicahya
- 4,205
- 7
- 36
- 56
-
1refer this http://docs.oracle.com/javase/tutorial/uiswing/events/changelistener.html – Sathish Oct 17 '12 at 13:02
-
2I second @kleopatra 's question. Sounds like you are trying to do something the wrong way. – Guillaume Polet Oct 17 '12 at 13:16
-
@kleopatra @ Guillaume Polet I found I needed it for the workaround for a "bug" reported here: https://bugs.openjdk.java.net/browse/JDK-5070992 Maybe that's what OP also was working on. – herrtim Nov 28 '16 at 16:51
3 Answers
7
Assuming you want to notify listeners of the slider, you would use this:
ChangeEvent ce = new ChangeEvent(slider);
for(ChangeListener cl : slider.getChangeListeners()){
cl.stateChanged(ce);
}
You shouldn't need to fire the change event directly unless you're extending the class and adding some new funky functionality. In that case, the fireStateChanged()
method is protected, so you should have access.

Nick Rippe
- 6,465
- 14
- 30
-
while technically correct, I'm really reluctant to upvote: if firing the change manually seems to be needed, most probably something is really, really wrong somewhere else. – kleopatra Oct 17 '12 at 13:35
-
@kleopatra: This [scenario](http://stackoverflow.com/q/2159803/230513) might be a reasonable use-case. – trashgod Oct 17 '12 at 16:42
-
@trashgod might be - hard to tell without context and a tad improbable :-) – kleopatra Oct 18 '12 at 11:40
-
@kleopatra: On reflection, this approach (implicitly) short-circuits the loose coupling afforded by `ChangeEvent`. I'd say it's a trade-off for closely related listeners. – trashgod Oct 18 '12 at 14:04