3

So I have two JSliders each of which invokes the stateChanged() method and I want to figure out which of the sliders invokes it.

In Objective-C, I used to use the tag property to differentiate which is which. Is there such an equivalent in Java ?

Sami Farhat
  • 1,164
  • 8
  • 12

2 Answers2

3

Tags are not normally used when determining the source component. You could use ChangeEvent.getSource() derived from the EventObject

public void stateChanged(ChangeEvent event) {
   JSlider source = (JSlider)event.getSource();
   ...
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 1
    yep i already use "(JSlider)event.getSource();" to get the callee of method of the stateChanged method, however what i want is to know which Slider ( 'A' or 'B' ) called it and deal with each differently. Hope I made sense :) – Sami Farhat Dec 26 '12 at 22:48
3

I'm no Java guy, but I believe setName() and getName() could be used for this. Perhaps double-check that these aren't used internally.

java.awt.Component

shawnwall
  • 4,549
  • 1
  • 27
  • 38