1 Importjava.beans.PropertyChangeEvent;2 ImportJava.beans.PropertyChangeListener;3 ImportJava.beans.PropertyChangeSupport;4 5 Public classDemobeans {6 7 PrivateString Demoname; 8 PrivateString Version; 9Propertychangesupport listeners =NewPropertychangesupport ( This); Ten One PublicDemobeans () { ADemoname = "InitValue"; -version= "v1000"; - } the - - PublicString Getdemoname () { - returnDemoname; + } - + A Public voidsetdemoname (String demoname) { atString OldValue = This. Demoname; - This. Demoname =Demoname; - //Release Listener Events -Firepropertychange ("Demoname", OldValue, demoname); - - } in - Public voidsetdemoversion (String demoversion) { toString oldversion = This. Version; + This. Version =demoversion; - //Release Listener Events theFirepropertychange ("Demoversion", Oldversion, demoversion); * $ } Panax Notoginseng Public voidAddpropertychangelistener (PropertyChangeListener listener) { - Listeners.addpropertychangelistener (listener); the } + A Public voidRemovepropertychangelistener (PropertyChangeListener listener) { the Listeners.addpropertychangelistener (listener); + } - $ $ /** - * Events that trigger attribute changes - */ the protected voidFirepropertychange (String prop, Object OldValue, Object newvalue) { - Listeners.firepropertychange (prop, OldValue, newvalue); Wuyi } the Public Static voidMain (string[] args) { -Demobeans beans =NewDemobeans (); Wu - Beans.addpropertychangelistener ( About NewPropertyChangeListener () { $ Public voidPropertyChange (propertychangeevent evt) { - if(Evt.getpropertyname (). Equals ("Demoversion")){ -System.out.println ("Version_oldvalue:" +Evt.getoldvalue ()); -System.out.println ("Version_newvalue:" +Evt.getnewvalue ()); ASystem.out.println ("Version_tpropertyname:" +evt.getpropertyname ()); + } the Else - { $System.out.println ("Name_oldvalue:" +Evt.getoldvalue ()); theSystem.out.println ("Name_newvalue:" +Evt.getnewvalue ()); theSystem.out.println ("Name_tpropertyname:" +evt.getpropertyname ()); the } the - }}); in the theBeans.setdemoname ("Test"); AboutBeans.setdemoversion ("v2000"); the the } the}
Java.beans.PropertyChangeListener