下面我写了一个简单的例子,其实就是一般的JavaBeans应用,你可以根据需要自己按照这个思路去改动。希望可以满足你的需要。
package _0322;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeNodeListenterTest {
public static void main(String[] args) {
MyTreeNode treeNode = new MyTreeNode("Node", 1);
treeNode.addDataLitenter(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("I am Change!");
}
});
treeNode.setData(4);
}
}
class MyTreeNode extends DefaultMutableTreeNode{
private int data;
private PropertyChangeSupport dataChange = new PropertyChangeSupport(this);
public MyTreeNode(Object userObject, int data) {
super(userObject);
this.data = data;
}
public void setData(int data)
{
int oldData = this.data;
this.data = data;
dataChange.firePropertyChange("data", oldData, data);
}
public void addDataLitenter(PropertyChangeListener listenter)
{
dataChange.addPropertyChangeListener(listenter);
}
public void removeDataLiterter(PropertyChangeListener listenter)
{
dataChange.removePropertyChangeListener(listenter);
}
}