The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.beans  [12 examples]

e5. Listing the Property Names of a Bean

This example determines the properties of a bean with the type MyBean.
    try {
        BeanInfo bi = Introspector.getBeanInfo(MyBean.class);
        PropertyDescriptor[] pds = bi.getPropertyDescriptors();
        for (int i=0; i<pds.length; i++) {
            // Get property name
            String propName = pds[i].getName();
        }
        // class, prop1, prop2, PROP3
    } catch (java.beans.IntrospectionException e) {
    }
    
    public class MyBean {
        // Property prop1
        public String getProp1() {
            return null;
        }
        public void setProp1(String s) {
        }
    
        // Property prop2
        public int getProp2() {
            return 0;
        }
        public void setProp2(int i) {
        }
    
        // Property PROP
        public byte[] getPROP3() {
            return null;
        }
        public void setPROP3(byte[] bytes) {
        }
    }

 Related Examples
e1. The Quintessential Bean
e2. Implementing a Bound Property
e3. Implementing a Constrained Property
e4. Instantiating a Bean
e6. Getting and Setting a Property of a Bean

See also: Events    Serialization   


© 2002 Addison-Wesley.