Vous êtes sur la page 1sur 6

Creating a javabean using Netbeans IDE

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


All beans must contain a constructor with no parameters. My constructor initializes my only property/member variable (img). All beans must contain 'implements Serializable'. You don't need to override any of these functions but you have to have that in your class definition. Serializable lets the IDE save your property values during design time. Any properties of your bean must have get and set functions if you want to have them show up in property lists. I only have one property and it is write only so I created a function called 'setImage()'. Beans apparently need to meet security considerations. so probably won't let you do certain things like access local files. I wanted a button with a cool image so I inherited from JButton. This is very basic implementation - there is no focus image, no down image, no roll over image. Here's the code:

//program package grlib1; import javax.swing.JButton; import java.awt.*; import java.io.Serializable; 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 step1. 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.

Vous aimerez peut-être aussi