PropertyChangeSupportの使用

1599 ワード

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Test01
{
	
    
	private String name;
	/*
	 *  PropertyChangeSupport , 
	 *  、 
	 */
	private PropertyChangeSupport listernts=new PropertyChangeSupport(this);
	
	/**
	 *  , PropertyChangeSupport 
	 * @param l
	 */
	public void addListner(PropertyChangeListener l)
	{
		listernts.addPropertyChangeListener(l);
	}
	
	/**
	 *  
	 * @param l
	 */
	public void removeListner(PropertyChangeListener l)
	{
		listernts.removePropertyChangeListener(l);
	}

	public String getName()
	{
		return name;
	}

	/**
	 *  , PropertyChangeSupport 
	 * @param name
	 */
	public void setName(String name)
	{
		this.name = name;
		listernts.firePropertyChange(null,null,getName());
	}
}
 import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class Test03 implements PropertyChangeListener
{

	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		System.out.println(" , ");
	}
	
	public static void main(String args[])
	{
		Test01 t01=new Test01();
		Test03 t02=new Test03();
		// , propertyChange 
		t01.addListner(t02);
		t01.setName("cxm");
	}

}