how can I make my own changeListener?
Hello,
I have this component - A clas Spin that has 3 spinners:
package component;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JSpinner;
import java.awt.Dimension;
publicclass Spinextends JPanel{
privatestaticfinallong serialVersionUID = 1;
private JSpinner js1 =null;
private JSpinner js2 =null;
private JSpinner js3 =null;
/**
* This is the default constructor
*/
public Spin(){
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
privatevoid initialize(){
this.setSize(150, 20);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.setPreferredSize(new Dimension(getSize().width, getSize().height));
this.add(getJs1(),null);
this.add(getJs2(),null);
this.add(getJs3(),null);
}
public JSpinner getJs1(){
if(js1==null){
js1=new JSpinner();
}
return js1;
}
public JSpinner getJs2(){
if(js2==null){
js2=new JSpinner();
}
return js2;
}
public JSpinner getJs3(){
if(js3==null){
js3=new JSpinner();
}
return js3;
}
public String getValue(){
return js1.getValue()+":"+js2.getValue()+":"+js3.getValue();
}
}
So if I want to catch a change of one of these 3 spinners I just need to do this:
js1.addChangeListener(new javax.swing.event.ChangeListener(){
publicvoid stateChanged(javax.swing.event.ChangeEvent e){
System.out.println("stateChanged()");// TODO Auto-generated Event stub stateChanged()
}
});
js2.addChangeListener(new javax.swing.event.ChangeListener(){
publicvoid stateChanged(javax.swing.event.ChangeEvent e){
System.out.println("stateChanged()");// TODO Auto-generated Event stub stateChanged()
}
});
js3.addChangeListener(new javax.swing.event.ChangeListener(){
publicvoid stateChanged(javax.swing.event.ChangeEvent e){
System.out.println("stateChanged()");// TODO Auto-generated Event stub stateChanged()
}
});
But I DONT need this above, I need this:
Spin mySpin =new Spin();
mySpin.addChangeListener(new javax.swing.event.ChangeListener(){
publicvoid stateChanged(javax.swing.event.ChangeEvent e){
Spin s = (Spin)e.getSource();
System.out.println(s.getValue());// TODO Auto-generated Event stub stateChanged()
}
});
What all I need to do that my spinner "Spin" can have its OWN changeListener so everytime I change ONE of these 3 spinners(doesnt matter which one) that MY changeListener can catch it and execute what ever I to.
I hope that it make sense what just asked..
Thanks for any ides.

