1.1.
Property Change event and voting veto event
The Propertychangesupport class provides a convenient way for Java beans to support property-altering events.
/*** @Title: eventbean.java* @Package com.test.javatechnology.introspection* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 25, 2017 PM 6:02:36*@versionV1.0*/ Packagecom.test.javatechnology.introspection;ImportJava.beans.PropertyChangeListener;ImportJava.beans.PropertyChangeSupport;/*** @ClassName: eventbean* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 25, 2017 PM 6:02:36**/ Public classEventbean { PublicEventbean () {}//defines property Change event support. PrivatePropertychangesupport changes =NewPropertychangesupport ( This); /*** Add Property Change Event listener. * @paramPropertyName The name of the property to be monitored *@paramListener Event Listener. */ Public voidAddListener (String Propertyname,propertychangelistener listener) {Changes.addpropertychangelistener ( PropertyName, listener);}/*** Remove Property Change Event listener. * @paramPropertyName The name of the property to be monitored *@paramListener Event Listener. */ Public voidRemoveListener (String Propertyname,propertychangelistener listener) {Changes.removepropertychangelistener ( PropertyName, listener);} //the Unique property. Private intvalue; Public intGetValue () {returnvalue;} //The departure event when the property is changed. Public voidSetValue (intvalue) {Try{intOldValue = This. Value;//triggers a property change event. Changes.firepropertychange ("Value", OldValue, value); This. Value =value;}Catch(Exception e) {//This property assignment operation is ignored when an exception occurs in the event listener. e.printstacktrace ();}} } //the code for adding an event listener is as follows://creates an instance of a Java bean. Eventbean obj=NewEventbean ();//adds an attribute to alter the event listener. Obj.addlistener ("Value",NewPropertyChangeListener () {@Override Public voidPropertyChange (propertychangeevent evt) {//the name of the output property, the old value, and the new value. System.out.println ("PropertyChange:" + "property:" +Evt.getpropertyname ()+ "OldValue:" +Evt.getoldvalue ()+ "NewValue:" +Evt.getnewvalue ());} }); //sets the new value. Obj.setvalue (1); Obj.setvalue (100);
The results of the operation are as follows:
PropertyChange:property:value oldvalue:0 newvalue:1
PropertyChange:property:value oldvalue:1 newvalue:100
use the veto voting listener to block property change operations. the Vetoablechangesupport class provides a convenient polling listener.
support for rejecting voting listeners The Java Bean looks like this:
/*** @Title: eventbean.java* @Package com.test.javatechnology.introspection* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 25, 2017 PM 6:02:36*@versionV1.0*/ Packagecom.test.javatechnology.introspection;ImportJava.beans.PropertyChangeListener;ImportJava.beans.PropertyChangeSupport;ImportJava.beans.VetoableChangeListener;ImportJava.beans.VetoableChangeSupport;/*** @ClassName: eventbean* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 25, 2017 PM 6:02:36**/ Public classVetoableeventbean { PublicVetoableeventbean () {}//Define veto Change event supportPrivateVetoablechangesupport vetoablechanges =NewVetoablechangesupport ( This); Public voidAddListener (String Propertyname,vetoablechangelistener listener) {Vetoablechanges.addvetoablechangelistener ( Propertyname,listener);} Public voidRemoveListener (String Propertyname,vetoablechangelistener listener) { Vetoablechanges.removevetoablechangelistener (Propertyname,listener);} //defines property Change event support. PrivatePropertychangesupport changes =NewPropertychangesupport ( This); /*** Add Property Change Event listener. * @paramPropertyName The name of the property to be monitored *@paramListener Event Listener. */ Public voidAddListener (String Propertyname,propertychangelistener listener) {Changes.addpropertychangelistener ( PropertyName, listener);}/*** Remove Property Change Event listener. * @paramPropertyName The name of the property to be monitored *@paramListener Event Listener. */ Public voidRemoveListener (String Propertyname,propertychangelistener listener) {Changes.removepropertychangelistener ( PropertyName, listener);} //the Unique property. Private intvalue; Public intGetValue () {returnvalue;} //The departure event when the property is changed. Public voidSetValue (intvalue) {Try{intOldValue = This. Value;//departure polling events. Vetoablechanges.firevetoablechange ("Value", OldValue, value); //triggers a property change event. Changes.firepropertychange ("Value", OldValue, value); This. Value =value;}Catch(Exception e) {//This property assignment operation is ignored when an exception occurs in the event listener. e.printstacktrace ();}} }
The example code for the voting veto listener is as follows:
/*** @Title: vetoablelistenertest.java* @Package com.test.javatechnology.introspection* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 25, 2017 PM 6:27:53*@versionV1.0*/ Packagecom.test.javatechnology.introspection;Importjava.beans.PropertyChangeEvent;ImportJava.beans.PropertyChangeListener;Importjava.beans.PropertyVetoException;ImportJava.beans.VetoableChangeListener;/*** @ClassName: vetoablelistenertest* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 25, 2017 PM 6:27:53**/ Public classVetoablelistenertest {/** * @paramargs*/ Public Static voidMain (string[] args) {//creates an instance of a Java bean. Vetoableeventbean obj=NewVetoableeventbean ();//Add a veto voting event listener. Obj.addlistener ("Value",NewVetoablechangelistener () {@Override Public voidVetoablechange (propertychangeevent evt)throwspropertyvetoexception {System.out.println ("Veto:" + "property:" +Evt.getpropertyname ()+ "OldValue:" +Evt.getoldvalue ()+ "NewValue:" +Evt.getnewvalue ()); //deprecated when the property value is 3 o'clock. if(Evt.getnewvalue (). Equals (3)){Throw NewPropertyvetoexception ("New value must not equal 3\r\n", evt);}} }); //adds an attribute to alter the event listener. Obj.addlistener ("Value",NewPropertyChangeListener () {@Override Public voidPropertyChange (propertychangeevent evt) {//the name of the output property, the old value, and the new value. System.out.println ("PropertyChange:" + "property:" +Evt.getpropertyname ()+ "OldValue:" +Evt.getoldvalue ()+ "NewValue:" +Evt.getnewvalue ());} }); //sets the new value. Obj.setvalue (1); System.out.println ("Value:" +Obj.getvalue ()); //will not changeObj.setvalue (3); System.out.println ("Value:" +Obj.getvalue ()); } }
When the new value is 3 in the event handling of the polling listener , the assignment is blocked by throwing a vetoexception exception.
The results of the operation are as follows:
Veto:property:value oldvalue:0 newvalue:1
PropertyChange:property:value oldvalue:0 newvalue:1
Value:1
Veto:property:value oldvalue:1 Newvalue:3
java.beans.PropertyVetoException: New value must not equal 3
Value:1
At Com.test.javatechnology.introspection.vetoablelistenertest$1.vetoablechange (VetoableListenerTest.java:44 )
At Java.beans.VetoableChangeSupport.fireVetoableChange (Unknown Source)
At Java.beans.VetoableChangeSupport.fireVetoableChange (Unknown Source)
At Java.beans.VetoableChangeSupport.fireVetoableChange (Unknown Source)
At Com.test.javatechnology.introspection.VetoableEventBean.setValue (vetoableeventbean.java:75)
At Com.test.javatechnology.introspection.VetoableListenerTest.main (vetoablelistenertest.java:68)
Java Key Technologies (15) Introspection property Change event and voting veto event