javascript
Brief description  about Online courses   join in Online courses
OR

what is Java Beans?

Mukul  Gosavi
Mukul Gosavi
Sr software Engineer

Java Beans is a specification developed by Sun Microsystems that defines how Java objects interact which can be used by any
application that understands the JavaBeans format.it is similar to an ActiveX control The principal difference between
ActiveX controls and JavaBeans are that ActiveX controls can be developed in any programming language but executed only on
a Windows platform, whereas JavaBeans can be developed only in Java, but can run on any platform.
A Javabean is just a java class  with the following requirements.

   i) It has a public no-args constructor

  ii) It has 'set' and 'get' methods for its properties.

  iii) It may have any general functions.

  iv) If required it must be Serializable.

JavaBeans is an object-oriented programming interface from Sun Microsystems that lets you build re-useable applications or
program building blocks called components that can be deployed in a network on any major operating system platform. Like
Java applets, JavaBeans components can be used to give World Wide Web pages or other applications interactive capabilities
such as computing interest rates or varying page content based on user or browser characteristics.

JavaBeans is a portable, platform-independent
component model written in the Java programming
language
● With the JavaBeans API you can create reusable,
platform-independent components
● Using JavaBeans-compliant application builder
tools such as NetBeans or Eclipse, you can
combine these components into applets,
applications, or composite components

Example: SimpleBean

import java.awt.Color;
import java.beans.XMLDecoder;
import javax.swing.JLabel;
import java.io.Serializable;
public class SimpleBean extends JLabel
implements Serializable {
public SimpleBean() {
setText( "Hello world!" );
setOpaque( true );
setBackground( Color.RED );
setForeground( Color.YELLOW );
setVerticalAlignment( CENTER );
setHorizontalAlignment( CENTER );
}
}

Example: XML Representation

<?xml version="1.0" encoding="UTF-8" ?>
<java>
<object class="javax.swing.JFrame">
<void method="add">
<object class="java.awt.BorderLayout" field="CENTER"/>
<object class="SimpleBean"/>
</void>
<void property="defaultCloseOperation">
<object class="javax.swing.WindowConstants"
field="DISPOSE_ON_CLOSE"/>
</void>
<void method="pack"/>
<void property="visible">
<boolean>true</boolean>
</void>
</object>
</java>

Write your comment now