FaqFormExtendingForms

Can I create forms by extending other forms?

Let's suppose you want to create one form to be a base (super class) for other forms (extending the base one). You can create the base form as usual, but it should match the requirements described in Creating a custom container bean :

  • it must be a bean (with public constructor without parameters),
  • it must be compiled at the time when used (i.e. when editing the extending forms),
  • it must be either empty (no components) or have an empty container delegate defined.

To create a form that extends the base form, use the Bean Form template and set its superclass to the base form class.

If you are surprised by seeing an empty form in the GUI builder while the base form had some sub-components, it can be caused by one of these two reasons:

  1. There is no empty container delegate defined, so the GUI builder clears the original container. See the Creating a custom container bean entry how to specify the container delegate. Make sure the BeanInfo class is correct and compiled.
  2. Or you see just the container delegate itself. If the form is of a Window type, before NetBeans 7.4 the GUI builder could not display it directly in the design area, and so only showed the container delegate (empty panel). Since NetBeans 7.4 the GUI builder can show complete content of JDialog and JFrame super class.

Note:

  • If the base form class is a JFrame, make sure its defaultCloseOperation property is set to DO_NOTHING (if set to EXIT, the class could not be instantiated for the extending form).
  • You can add new components to the extending form and change the properties exposed by the base form class.
  • It is not possible to change the sub-components and the layout of the base form from the extending form.
  • When designing the extending form, you'll see the name of the base form class in the Inspector tree (it shows the class that is being extended).
  • Any changes made in the extending form won't affect the base form class.
  • Changes made in the base form will affect all forms extending it.

Tip for iterative development:

If you change something in the base form, don't forget to compile it. If there are some extending forms opened at the same time, they won't be updated automatically, you must reopen them manually to load the new base class (to do it quickly, invoke 'Reload Form' action on the root form node in Inspector).

Example

Download this example (zipped NetBeans project showing how to create a base dialog class and extend it visually in GUI builder).


  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