Contents
This document
What is JBeans?
Why use JBeans?
What does JBeans replace?
Making life easier
Installation
Building
Case studies/scenarios
First steps/definitions
Using BeanProperty
Using IndexedBeanProperty
Using NestedBeanProperty
Using DynamicNestedBeanProperty
Using JavaBean
Events and conversion
Using IndexedBeanProperty

IndexedBeanProperty is used to access local indexed properties. This class functions identically to the BeanProperty class except that it provides access to indexed properties rather than normal properties. If you recall from above, indexed properties take an extra parameter that is an integer index. This means that the property access methods (getPropertyValue and setPropertyValue) on the IndexedBeanProperty class will also take an extra parameter, the index. Other than this, everything that we mentioned above for the BeanProperty holds true for this class, including the exceptions.

We will skip the discussion about the constructors of the IndexedBeanProperty because they are identical to the constructors for the BeanProperty. Instead we will move on to the access methods. Here are the new getPropertyValue and setPropertyValue methods:

    public Object getPropertyValue(Object beanInstance, int index) 
    throws BeanException;
    
    public void setPropertyValue(Object beanInstance, int index, Object value) 
    throws BeanException, ConversionException;
    
    public void setPropertyValue(Object beanInstance, int index, Object value, boolean convert) 
    throws BeanException, ConversionException;
    

As you can see, these methods are identical to the methods for the BeanProperty, except that they all take an new integer parameter which is the index. This parameter will be passed along to the get and set methods on the Java bean properties.

Here are a few examples of using the IndexedBeanProperty class to access Java bean properties:

    /* We'll use this Java bean for our example */
    public class Lottery {
        private int [] numbers = new int[6];
        
        public int getNumber(int index) { return numbers[index]; }
        public void setNumber(int index, int value) { numbers[index] = value; }
    }
    
    // Retrieving a lotto number using IndexedBeanProperty
    Lottery lotto = new Lottery();
    lotto.setNumber(0, 32);
    
    try {
        IndexedBeanProperty property = new IndexedBeanProperty("number", Lottery.class);
        
        // Gets the lotto number from the Lottery instance
        Integer number = (Integer) property.getPropertyValue(lotto, 0); 
        System.out.println("The first lotto number is: " + number);
    } catch (BeanException be) {
        be.printStackTrace();
    }
    
    /*
    Output:
    The first lotto number is: 32
    */
    

In this example, you can see that the number property of the Lottery Java bean instance is being retrieved using the IndexedBeanProperty class. Notice that the index used to setup the number on the Lottery instance and the index used to retrieve the number using the IndexedBeanProperty are the same and this results in the same number that was set, being retrieved.

    // Setting a lotto number using IndexedBeanProperty
    Lottery lotto = new Lottery();
    
    try {
        IndexedBeanProperty property = new IndexedBeanProperty("number", Lottery.class);
        property.setPropertyValue(lotto, 0, new Integer(42));
        System.out.println("The first lotto number is: " + lotto.getNumber(0));
    } catch (BeanException be) {
        be.printStackTrace();
    }
    
    /*
    Output:
    The first lotto number is: 42
    */
    

In this example, you can see that the number property of the Lottery Java bean instance is being changed using the IndexedBeanProperty class. Like the retrieval example above, the index used to change and retrieve the property are the same, which results in the same lotto number.

As you can tell, the IndexedBeanProperty and BeanProperty classes serve the same purpose for indexed and normal properties respectively.