Chapter 9. JavaBeans

Why Should I Make Beans?

You should make JavaBeans to encourage reuse of your Java code. JavaBeans is the standard architecture for building reusable components in Java. Originally intended as visual components for building GUIs, now the JavaBeans are widely used in all Java environments, including JSP and EJB development. Like components in other object-oriented programming languages, JavaBeans have properties that can be set at design-time by visual design tools or at runtime by the containing program.

If you want other developers to use your code, if you want your components to snap together with ease, if you want to spend your time developing application logic instead of debugging interfaces, make JavaBeans. For a deeper understanding of the JavaBeans architecture, see Sun’s JavaBeans Technology (http://java.sun.com/beans) page or read Developing JavaBeans by Robert Englander (O’Reilly).

Creating JavaBeans

We will continue with the AddStrings example from the last chapter to demonstrate NetBeans features for developing JavaBeans. It is not necessary to go through all this to create a Bean from scratch. Simply follow the procedure for creating a new Java object, and use the Bean template. We’re taking the long route to show more ways that NetBeans can help. Here’s the plan:

  1. Create a GUI component from the AddStrings JFrame.

  2. Convert the component into a proper Bean.

  3. Add an event set to inform the container about changes to Bean properties.

  4. Generate a BeanInfo ...

Get NetBeans: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.