Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between PropertyChangeListener and VetoableChangeListener?

Tags:

java

Everything is in the title of the question. Can you provide some use case we use PropertyChangeListener and VetoableChangeListener ?

like image 484
Dimitri Avatar asked May 11 '10 14:05

Dimitri


People also ask

What is Vetoable change in java?

Vetoable property changes. A VetoableChangeEvent for a constrained property should be fired before the actual value of the property has changed. This gives any VetoableChangeListener a chance to reject the change when its vetoableChange() method is called. The event source must catch the java.

What is meant by constrained property?

A constrained property is a special kind of bound property. For a constrained property, the bean keeps track of a set of veto listeners. When a constrained property is about to change, the listeners are consulted about the change.


2 Answers

The main difference resides in the fact that PropertyChangeListener are applied to bound properties while VetoableChangeListener are applied to constrained properties.

A bound property is just a property, while a constrained property is a property on which listeners can express themselves about a change that is going to be made: they can refuse this change from happening.

What it actually happens is that when you notify a vetoable property change you will do something like

VetoableChangeSupport vcs;
vcs.fireVetoableChange(...);

and this can throw a PropertyVetoException which will tell your bean that an observer wishes to block this property change (it should be rolled back).

like image 193
Jack Avatar answered Sep 22 '22 15:09

Jack


A VetoableChangeListener can veto (forbid) the property change. It will be rolled back if the receiver wishes. You may also attach constraints to the changed property.

like image 27
Simon Avatar answered Sep 21 '22 15:09

Simon