Listen for a constrained property change : Java Beans « Development Class « Java






Listen for a constrained property change

    

 
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
 
public class Main {
    private double interest;
 
    private VetoableChangeSupport vcs = new VetoableChangeSupport(this);
 
    public Main() {
        vcs.addVetoableChangeListener(new VetoChangeListener());
    }
 
    public double getInterest() {
        return interest;
    }
 
    public void setInterest(double interest) {
        try {
            vcs.fireVetoableChange("interest", new Double(this.interest), new Double(interest));
             this.interest = interest;
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        Main bean = new Main();
        bean.setInterest(10.99);
        bean.setInterest(15.99);
        
        bean.setInterest(20.99);
    }
}
 
class VetoChangeListener implements VetoableChangeListener {
    public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
        String eventName = evt.getPropertyName();
        if (eventName.equalsIgnoreCase("interest")) {
            double interest = ((Double) evt.getNewValue()).doubleValue();
            if (interest > 20.00) {
                throw new PropertyVetoException("Interest must be below 20.00", evt);                
            }
            System.out.println("Interest applied = " + interest);
        }
    }
}
 

   
    
    
    
  








Related examples in the same category

1.JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2.JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3.JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4.JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5.JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6.Bean ContainerBean Container
7.PropertyTablePropertyTable
8.Introspecting a BeanIntrospecting a Bean
9.Listening for Changes to the Selected File in a JFileChooser Dialog
10.Get a list of selected files
11.Listening for Changes to the Current Directory in a JFileChooser Dialog
12.Displaying the Current Directory in the Title of a JFileChooser Dialog
13.Setting an Accessory Component in a JFileChooser Dialog
14.Convert a bean to XML persistence
15.Listen for bean's property change event
16.List property names of a Bean
17.Prevent bean's property being serialized to XML
18.Create an instance a Bean
19.Convert an XML persistence to bean
20.Determine bean's property type
21.Bean has a single property called property.
22.Implementing a Bound Property
23.Implementing a Constrained Property: fires a PropertyChangeEvent whenever its value is about to be changed.
24.Instantiating a Bean
25.Listing the Property Names of a Bean
26.Getting and Setting a Property of a Bean
27.Get and set the value of a property in a bean using Expression and Statement
28.Get and set an Object type property
29.gets and sets a primitive type property
30.gets and sets an array type property
31.Serializing a Bean to XML: XMLEncoder only persists the value of public properties.
32.Deserializing a Bean from XML
33.Preventing a Bean Property from Being Serialized to XML
34.Serializing an Immutable Bean Property to XML
35.Listening for a Property Change Event: A property change event is fired when a bound property is changed.
36.Listening for a Vetoable Property Change Event
37.Read bean's property value
38.Saving and restoring the state of a pretend CAD systemSaving and restoring the state of a pretend CAD system
39.An extension of ArrayList that provides some handy utilities for working with JavaBeans
40.An extension of Vector that provides some handy utilities for working with JavaBeans
41.extends SimpleBeanInfo
42.Get and set properties on a bean
43.Bean Utility
44.Is JavaBean Compliant Setter
45.This program demonstrates the use of an XML encoder and decoder to save and restore a frame.
46.This program demonstrates various persistence delegates.
47.Event Tracer