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" defined, i.e. a subcontainer that is empty and has some known layout manager. The container delegate can be specified by setting containerDelegate value to the 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 and JDialog have getContentPane here, so be careful not to use the same method when extending JFrame or JDialog).

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

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

Note 2: For an opposite situation, i.e. if need 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