Skip to main content

JavaBeans: feature attributes

Posted by malenkov on October 4, 2007 at 7:29 AM PDT

Lets talk about FeatureDescriptor, the superclass for all other descriptors. It enables to add extra metadata to describe the beans. The builder tool could use such metadata to provide enhanced functionality. You can use the getValue(String) method to retrieve metadata. For example:

BeanInfo info = Introspector.getBeanInfo(JLabel.class);
Object value = info.getBeanDescriptor().getValue("isContainer");
boolean isContainer = (value instanceof Boolean) && (Boolean)value;

The following attributes are supported in JDK. Other beans creators can use their own attributes and support them in own builder tools. See JavaBean BeanInfo Attributes and JavaBeans in JFormDesigner articles for details and examples on how to use attributes.

isContainer

The attribute must be a Boolean value in BeanDescriptor. It specifies whether a component is a container or not (a container can have child components).

isContainer was introduced primarily for the Swing library. All Swing components inherit the Container class by design, so for the builder tool we assume that all Swing components are containers. Swing library provides BeanInfo classes with the isContainer attribute to fix this problem.

containerDelegate

The attribute must be a String value in BeanDescriptor. It specifies the name of a method to retrieve an actual container for any subcomponents of the bean. This method should not have any parameters and should return an instance of the Container class. For example: JScrollPane.getViewport().

persistenceDelegate

The attribute must be a PersistenceDelegate instance in BeanDescriptor. It specifies an instance of a class that can be used to persist the described bean.

By default every bean could be encoded with the DefaultPersistenceDelegate class, howewer, some classes requires special processing. For example, the HashMap object should be encoded, but its content cannot be accessed through a property getter and setter.

enumerationValues

The attribute must be an array of Object values in PropertyDescriptor. It specifies a list of valid property values. For each property value, the array must contain three items:

  • Name — A displayable name for the property value.
  • Value — The actual property value.
  • Java Initialization String — A Java code piece used for code generating.

For example, the orientation property of the JSlider class contains the following enumeration values:

new Object[] {
    "horizontal", Integer.valueOf(JSlider.HORIZONTAL), "JSlider.HORIZONTAL",
    "vertical",   Integer.valueOf(JSlider.VERTICAL),   "JSlider.VERTICAL",
}

transient

The attribute must be a Boolean value in PropertyDescriptor. It specifies whether the property value should not be persisted and no code should be generated. For example, we should not encode the location property of the Point class to avoid the StackOverflowError.

Related Topics >>