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.

[6041 byte] By [johny480a] at [2007-10-3 3:26:33]
# 1

So create a single listener and add it to all three spinners:

ChangeListener spinListener = new javax.swing.event.ChangeListener() {

public void stateChanged(javax.swing.event.ChangeEvent e) {

Spin s = (Spin)e.getSource();

System.out.println(s.getValue()); // TODO Auto-generated Event stub stateChanged()

}

});

js1.addChangeListener(spinListener);

js2.addChangeListener(spinListener);

js3.addChangeListener(spinListener);

BinaryDigita at 2007-7-14 21:19:44 > top of Java-index,Desktop,Core GUI APIs...
# 2

BinaryDigit thx for replying but I dont need one Listener to all spinners INSIDE the class SPIN, I need that my class can use addChangeListener method.

exactly in the way I posted before. And we know that a class cant extend from (2 or more classes) a jpanel and a jspinner together.

So this is the problem

And another problem with your listener what you wrote is that in the moment that this :

Spin s = (Spin)e.getSource();

is executed an exception is thrown because source is JSpinner not my Spin class.

johny480a at 2007-7-14 21:19:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

Oh sorry about that, I misunderstood your question. I think I see now what you need to do. First create your own custom event class:

public class SpinChangeEvent {

private Spin source = null;

private Object value = null;

public SpinChangeEvent(Spin source, Object value) {

this.source = source;

this.value = value;

}

public Spin getSource() { return source; }

public Object getValue() { return value; }

}

Then create your own SpinChangeListener interface:

public interface SpinChangeListener {

public void stateChanged(SpinChangeEvent ev);

}

In your Spin class, maintain a list of SpinChangeListeners that will be notified of changes, and add regular javax.swing.event.ChangeListeners to your JSpinners to do the notification:

private ArrayList<SpinChangeListener> listeners = new ArrayList<SpinChangeListener>();

private void initialize() {

// Do other init stuff, then this:

javax.swing.event.ChangeListener lis = new javax.swing.event.ChangeListener() {

public void stateChanged(ChangeEvent e) {

doSpinnerChanged();

}

};

js1.addChangeListener(lis);

js2.addChangeListener(lis);

js3.addChangeListener(lis);

}

// This gets called by each spinner when it changes.

private void doSpinnerChanged() {

// Create a new SpinChangeEvent and notify all the listeners.

SpinChangeEvent event = new SpinChangeEvent(this, getValue());

for (SpinChangeListener listener : listeners) {

listener.stateChanged(event);

}

}

// These are for outside classes to register themselves as listeners for SpinChangeEvents:

public void addSpinChangeListener(SpinChangeListener lis) { listeners.add(lis); }

public void removeSpinChangeListener(SpinChangeListener lis) { listeners.remove(lis); }

Then whatever class uses the Spin object can register itself as a SpinChangeListener like this:

spin.addSpinChangeListener(new SpinChangeListener() {

public void stateChanged(SpinChangeEvent ev) {

Spin source = ev.getSource();

System.out.println(ev.getValue());

}

});

Output after several spinner changes is:

1:0:0

1:1:0

1:1:1

1:1:2

1:1:3

1:0:3

BinaryDigita at 2007-7-14 21:19:44 > top of Java-index,Desktop,Core GUI APIs...
# 4
BinaryDigit, cool, problem solved...Thank you very much.You just cant imagine how MUCH you helped me.
johny480a at 2007-7-14 21:19:44 > top of Java-index,Desktop,Core GUI APIs...