FaqFormCustomContainerBean

(Difference between revisions)
Line 10: Line 10:
* the default instance must have the layout set to some supported layout manager.
* 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 <tt>BeanDescriptor</tt> of the <tt>BeanInfo</tt> of the bean - this value should be the name of the method for obtaining the container delegate from the bean (e.g.<tt>JFrame</tt> has <tt>getContentPane</tt> here, so be careful not to use the same method when extending <tt>JFrame</tt>).
+
'''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 <tt>BeanDescriptor</tt> of the <tt>BeanInfo</tt> of the bean -- this value should be the name of the method for obtaining the container delegate from the bean (e.g.<tt>JFrame</tt> and <tt>JDialog</tt> have <tt>getContentPane</tt> here, so be careful not to use the same method when extending <tt>JFrame</tt> or <tt>JDialog</tt>).
Example of <tt>getBeanDescriptor()</tt> method in <tt>MyContainerBeanInfo</tt> (for <tt>MyContainer</tt> bean):
Example of <tt>getBeanDescriptor()</tt> method in <tt>MyContainerBeanInfo</tt> (for <tt>MyContainer</tt> bean):
<pre>
<pre>
-
public BeanDescriptor getBeanDescriptor() {
+
public class BaseDialogBeanInfo extends SimpleBeanInfo {
-
    BeanDescriptor desc = new BeanDescriptor(MyContainer.class);
+
    public BeanDescriptor getBeanDescriptor() {
-
    desc.setValue("containerDelegate", "getInnerPane");
+
        BeanDescriptor desc = new BeanDescriptor(MyContainer.class);
-
    return desc;
+
        desc.setValue("containerDelegate", "getInnerPane");
 +
        return desc;
 +
    }
}
}
</pre>
</pre>
-
'''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 <tt>isContainer</tt> property in <tt>BeanDescriptor</tt> to <tt>Boolean.FALSE</tt> similarly as described above.
+
'''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 <tt>isContainer</tt> property in <tt>BeanDescriptor</tt> to <tt>Boolean.FALSE</tt> similarly as described above.
--------
--------

Revision as of 10:59, 2 May 2013

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 BaseDialogBeanInfo 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