FaqFormCustomContainerBean

Revision as of 16:51, 6 November 2009 by Admin (Talk | contribs)

How do I create a custom container bean?

The simplest (and recommended) way is to create a JPanel subclass.

There are the following requirements for a bean to be recognized and used as a visual container in the GUI builder:

  • the class must be a java.awt.Container subclass,
  • the default instance of the container must be empty (no subcomponents),
  • the default instance must have the layout set to some supported layout manager.

Note 1: It is possible to have some subcomponents in the container, but then it must have a "container delegate" subcontainer defined (this container delegate must be empty and have some layout). The container delegate can be specified by setting containerDelegate value to BeanDescriptor of the BeanInfo of the bean - this value should be the name of the method for obtaining the container delegate from the bean (e.g.JFrame has getContentPane here, so be careful not to use the same method when extending JFrame).

Example of getBeanDescriptor() method in MyContainerBeanInfo (for MyContainer bean):

public BeanDescriptor getBeanDescriptor() {
    BeanDescriptor desc = new BeanDescriptor(MyContainer.class);
    desc.setValue("containerDelegate", "getInnerPane");
    return desc;
}

Note 2: For an opposite situation, i.e. to have a container subclass which should be treated as non-container you don't need to care once it contains some components. (This violates the requirements from above which NetBeans ignores silently.) If you want to be quite correct, set the isContainer property in BeanDescriptor to Boolean.FALSE similarly as described above.


  Applies to: All NetBeans versions
  Platforms: All
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo