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
- Open the Palette Manager from main menu: Tools | Palette Manager | Swing/AWT Components
- 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
- 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:
- Select the java file of the component in the project explorer and invoke Copy (Ctrl-C).
- Open the GUI form where you want to add the component to. The form can be in the same or in another project.
- Select the desired target container in the form (either in the designer area or in Inspector).
- Invoke Paste (Ctrl-V).
- Adjust the component's position/size via mouse as needed.
You can also install the component to palette quickly without using Palette Manager:
- Select the java file of the component in the project explorer.
- From context menu choose Tools | Add to Palette...
- 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
- Use the Swing component ancestor in the GUI builder (e.g. JTable).
- Select the component, go to Properties window, select Code tab, Custom Creation Code property.
- 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