亲宝软件园·资讯

展开

java PropertyChangeSupport 详解java中的PropertyChangeSupport与PropertyChangeListener

人气:1
想了解详解java中的PropertyChangeSupport与PropertyChangeListener的相关内容吗,在本文为您仔细讲解java PropertyChangeSupport的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java中的PropertyChangeSupport与PropertyChangeListener的实例详解,PropertyChangeSupport与PropertyChangeListener,下面大家一起来学习吧。

详解java中的PropertyChangeSupport与PropertyChangeListener

java中的PropertyChangeSupport与PropertyChangeListener配合使用能够实现,绑定属性会在属性值发生变化时,通知所有相关的监听器。

javaBean:

package com.wang.test.property; 
 
import java.beans.PropertyChangeListener; 
import java.beans.PropertyChangeSupport; 
 
public class TestBean { 
 
  protected final PropertyChangeSupport support = new PropertyChangeSupport(this); 
   
  private String str; 
   
  public String getStr() { 
    return str; 
  } 
 
  public void setStr(String str) { 
    support.firePropertyChange("str", this.str, str); 
    this.str = str; 
  } 
   
  public void addPropertyChangeListener(PropertyChangeListener listener) { 
    support.addPropertyChangeListener(listener); 
  } 
   
  public void removePropertyChangeListener(PropertyChangeListener listener) { 
    support.removePropertyChangeListener(listener); 
  } 
} 

 client:

package com.wang.test.property; 
 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
 
public class PropertyChangeSupportTest { 
 
  public static void main(String[] args) {  
     
    TestBean test = new TestBean(); 
    test.setStr("ddddd"); 
    test.addPropertyChangeListener(new PropertyChangeListener1()); 
    test.setStr("yyyy"); 
     
    //test.addPropertyChangeListener(new PropertyChangeListener2()); 
    //test.setStr("hhhhhhf"); 
  }  
    
  public static class PropertyChangeListener1 implements PropertyChangeListener{ 
 
    @Override 
    public void propertyChange(PropertyChangeEvent evt) { 
      System.out.println( "1: " + evt.getPropertyName() + "  " + evt.getNewValue() + "  " + evt.getOldValue()); 
       
    } 
  } 
   
  public static class PropertyChangeListener2 implements PropertyChangeListener{ 
 
    @Override 
    public void propertyChange(PropertyChangeEvent evt) { 
      System.out.println( "2: " + evt.getPropertyName() + "  " + evt.getNewValue() + "  " + evt.getOldValue()); 
       
    } 
  } 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

加载全部内容

相关教程
猜你喜欢
用户评论