How to make a simple java bean using Netbeans 6.x.x (article originally written using Netbeans 6.0.1, but I tried this and it is still relevant for netbeans 6.5, netbeans 6.9)

I struggled with this. It used to be easier in Netbeans 5.5.1 but they took the functionality out of netbeans. Why? well the discussion is here:

http://wiki.netbeans.org/Beans_90907Experiment

I got most of my answers from this article here: http://java.sun.com/docs/books/tutorial/javabeans/writingbean/index.html

Step 1 - Create a library

From a fresh opening of netbeans, do:
file->new project
for category choose 'java'
for project choose 'java class library'
Then choose an appropriate location and name for your library. I chose grlib1 for the name.

Step 2 - Create an empty class

file->new file
for category choose 'java'
for file type choose 'Java Class'
click 'next'
Enter an appropriate class name. I chose 'grbuttonx'.
Leave everything else untouched.

Step 3 - Make code changes

I wanted a button with a cool image so I inherited from JButton. This gave me all kinds of properties and it already serializes everything I care about (don't care about the image as this is loaded in runtime). This is very basic implementation - there is no focus image, no down image, no roll over image. Here's my code:



package grlib1;
import javax.swing.JButton;
import java.awt.*;
import java.io.Serializable;


/**
 * @author gr@gr5.org
 */
public class grbuttonx extends JButton implements Serializable {
    private Image img;
    public grbuttonx() {
        img=null;
    }
    
    public void setImage(Image i) {
        img=i;
    }
    
     public void paint(Graphics g) {
        if (img != null)
            g.drawImage(img, 0, 0,null);
        else {
            g.setColor(Color.red);
            Dimension size = getSize();
            g.fillOval(0, 0, size.width, size.height);
            }
    }

}

Step 4 - Fix manifest to show this is a bean library

First get to the files tab of your project (ctrl/2 should get you there). In the top level of the tree view it shows your projects. The next level down should have a file called build.xml. Click the plus sign next to it. Then double click any element below build.xml. You should be now editing a file called build-impl.xml.

Search for "<manifest" (I include the open < to simplify the search). There should only be one such tag in the file and you should be in the jar building section (scroll up to see the next comment block if you want to double check).

There should already be 2 attributes in the manifest. Add one called Java-Bean as shown:


            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
                <attribute name="Class-Path" value="${jar.classpath}"/>
                <attribute name="Java-Bean" value="true"/>
            </manifest>


Now build your library!
 

Step 5 - test it!

Create a new project or open an existing project. Open a frame or a form or something that allows you to drop buttons onto it. Get into design mode so you can see the palette window (if you can't see it you can open the palette window and you might want to pin it open with the pin in the title bar).

In menu system do: tools -> palette -> Swing/AWT Components
there should be 3 buttons. Eventually you might want to choose Add from JAR but we will do:
Add From Project...
There may be a delay at this point. Navigate if necessary and choose your library project created in step 1. Click next.
Your component should show up. Click it and click 'next' again.
Select a folder to put your bean into - I always choose "Beans". Click 'finish'.

Now you should see your been in the pallette near the bottom in the Beans section. Open up the beans section if it isn't already. Click your bean and then click in your window to place it. That's it.

Step 6 - debugging

If you keep both projects open (the library and the project) you can usually make edits in either place and breakpoints in either place. I recommend keeping the non-library as the default/main project which runs if you hit F6 and keeping a window on your bean for edits and breakpoints. Before building your application, build just the bean with the bean code open and hit F9 key to build it. Occasionally you will want to build the whole library. I don't know the IDE well so I choose the library as the main project, build it, then go back to the application project. Occasionally I go completely out of the application and reopen it to get proper design time handling of my bean but usually I don't care if it looks like the pre-edited bean. Breakpoints work great.

Good luck! If this is useful to you please add a link to this page from one of your web pages so that this page will get higher on search order for site like google (but only do it if you like this document). Also please email me - I always like getting comments/suggestions/whatever. Really. If I get too much email I will just remove this link. You can email me here.