How can I use a custom subclass of a Swing component? Do I need to install it to palette?

It depends on how the custom component is available. The most common way is to use the Palette Manager to install the component to palette, but there is also shorter way if you have the source file part of a NetBeans project.

To install via Palette Manager

  1. Open the Palette Manager from main menu: Tools | Palette Manager | Swing/AWT Components
  2. In the Palette Manager press button according to where the component comes from. The choices are:
  • from an external JAR file
  • from a library defined in the IDE (always create a library if the components need more than one JAR)
  • from a NetBeans project
  1. Follow the installation wizard (3 steps):
  • select the source JAR file/library/project dir
  • select components to install
  • select the target category in palette (e.g. "Beans").

If you have the source file in a NetBeans project...

...you can even use the component without installing to palette:

  1. Select the java file of the component in the project explorer and invoke Copy (Ctrl-C).
  2. Open the GUI form where you want to add the component to. The form can be in the same or in another project.
  3. Select the desired target container in the form (either in the designer area or in Inspector).
  4. Invoke Paste (Ctrl-V).
  5. Adjust the component's position/size via mouse as needed.

You can also install the component to palette quickly without using Palette Manager:

  1. Select the java file of the component in the project explorer.
  2. From context menu choose Tools | Add to Palette...
  3. Choose the category to add the component in (e.g. "Beans").

Note: In all cases you must make sure the component is always correctly compiled at the moment when used in a form. Otherwise you get an error.

To use a Swing component subclass without instantiating it in the GUI builder

  1. Use the Swing component ancestor in the GUI builder (e.g. JTable).
  2. Select the component, go to Properties window, select Code tab, Custom Creation Code property.
  3. Invoke the editor and enter java code you'd like to have to instantiate the subclass. E.g.: new MyTable();
    Note you can also define a full anonymous innerclass here.

This can be handy if you need to subclass a Swing component in place for just a little modification, or if the component can't be instantiated as JavaBean.

  Applies to: NetBeans 5.0, 5.5, 6.x
  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