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
Why use JBeans?

As we have mentioned, one of the main goals of the JBeans project is to make life easier on the developer. Here are a few examples of how the JBeans package makes life easier than using the standard java.beans package. Most of the examples won't mean much to you until you have read through this entire document, but they do show how the amount of code required is greatly reduced. These examples are also very canned examples and in real situations you might not do anything like these examples.

 
Setting properties

Using the java.beans package:

    try {
        PropertyDescriptor pd = new PropertyDescriptor("name", Bean.class);
        
        Method read = pd.getWriteMethod();
        Object [] params = new Object [] {valueBeingSet};
        
        read.invoke(bean, params);
    } catch (IntrospectionException ie) {
        ...
    } catch (IllegalAccessException ie) {
        ...
    } catch (IllegalArgumentException ie) {
        ...
    } catch (InvocationTargetException ie) {
        ...
    }
    

Using the JBeans package:

    try {
        BeanProperty bp = new BeanProperty("name", Bean.class);
        bp.setPropertyValue(bean, valueBeingSet);
    } catch (BeanException ie) {
        ...
    }
    

 
Type conversion

Using the java.beans package:

    try {
        String strVersion = new String("1");
        Integer intVersion;
        
        PropertyEditor pe = PropertyEditorManager.findEditor(Integer.class);
        
        pe.setValueAsText(strVersion);
        intVersion = (Integer) pe.getValue();
    } catch (IllegalArgumentException ie) {
        ...
    }
    

Using the JBeans package:

    try {
        String strVersion = new String("1");
        Integer intVersion;
        
        TypeConverter tc = TypeConverterManager.getTypeConverter(Integer.class);
        
        intVersion = (Integer) tc.convert(strVersion, Integer.class);
    } catch (TypeConversionException tce) {
        ...
    }
    

 
More type conversion and property setting

Using the java.beans package:

    try {
        String strVersion = new String("16");
        Integer intVersion;
        
        PropertyEditor pe = PropertyEditorManager.findEditor(Integer.class);
        
        pe.setValueAsText(strVersion);
        intVersion = (Integer) pe.getValue();
        
        PropertyDescriptor pd = new PropertyDescriptor("age", Bean.class);
        
        Method read = pd.getReadMethod();
        Object [] params = new Object [] {intVersion};
        
        read.invoke(bean, params);
    } catch (IllegalArgumentException ie) {
        ...
    }
    

Using the JBeans package:

    try {
        String strVersion = new String("16");
        BeanProperty bp = new BeanProperty("age", Bean.class);
        bp.setPropertyValue(bean, strVersion, /*convert=*/true);
    } catch (BeanException ie) {
        ...
    }
    

As you can see, the JBeans package makes life much easier by reducing the amount of code required to access Java beans.