Javabeans : The Beans of the Future

Beans in the Market

If you go out in the market and enquire about the types of beans available, you are bound to get a variety of answers. There are french beans, large beans, small beans etc.,. and then some intelligent friend of your's might say that Javabeans are also available. Let us leave all the other beans to the vegetable vendors, cooks and mothers to figure out and talk of Javabeans. Truly speaking, javabeans at the first sight might take your minds to the ungrazed pastures of beans in the island Java(Indonesia)?????But it's not so. In short it has something to do with the much talked about programming language, Java.

Before knowing what a javabean is, let's discover the world of COMPONENTS and CONTAINERS . Seeing these BIG words used by shipment fellas, you don't have to get bogged down and think about the fettering consequences javabeans could have on your lives. You don't have to look out for some intravenous javabeans infusion in the market, since you'll be spared the time and money, searching for such an infusion. Patience, is what we ask of you . Just join us in our ride to ecstacy and experience the enlightenment that knowledge can endow upon you.

First let's get our basics clear with a true to life example.

Suppose we had two programmers and a client. Programmer A has come out with the latest E-MAIL program with a great user interface. The client hires A and is ready to buy his program. But fortunately or unfortunately the client is weak in English, and sends programmer A a letter saying

" I will take your prouduct only if you can inculude spelcheck in it."

Signed : client

Seeing the pitiable state of his client's English, A takes it to be his moral responsibility to include spellcheck in his program. He neither has the time nor the patience to sit down and write a speller-checker program. So, he goes to the market and buys a speller -checker from B and includes it in his E-MAIL program. Now B's program is the component , which is added to A's program(container). Not every programmer who wrote the speller-checker got the deal with A(container). Only that program, which complied with, or followed all the set of rules put forth by A got the deal.

We should drive home the fact that a COMPONENT must follow a certain set of rules given by the CONTAINER . Sun Microsystems developed a set of rules to enable java programs to be used as components, which they named 'The JavaBeans'. They also developed a reference container for these Javabeans, to show the world, the rules to be followed while using them.

Anatomy of a Bean :

For God's sake, this is not a botany tutorial??????? We are making our first attempts at studying the internal structure (anatomy) of a component (Javabean). We are dealing with the latest technology and we sincerely hope that you get this bean working. Let's jump into the

unfathomable / great

ocean full of
bewilderness / bliss.

Go through the entire tutorial and strike out whichever is not applicable. Create a subdirectory. Consult astrologers for giving it the right name, because we sincerely believe that "Well begun is half (the work) done".

We consulted Shri.Astro Maharaj (SAM) who laid our JavaBean Foundation Stone (JFS)???? by naming our subdirectory as jfs. We were even forced to print the legal statement inorder to keep ourselves litigation free. Legal Statement : JFS has no relation whatsoever with JFC .
All Rights Reserved by S.A.M copyright xxxxxx

Bare Minimum Necessities(BMN) for learning JavaBeans

  1. Windows 95
  2. JDK 1.1.x
  3. SCQ

The first two BMN's are heard of but, what is the third BMN ?Is it a new software in themarket? These are questions which usually cross the mind of any sensible man. The answer !! Oh! it's very simple. SCQ is what every man should have for surging ahead in life

A). It is an abbreviation where,

S- Sense of humor .
C- Courage, for accepting challenges .
And Q- a Querying mind .

c:\jfs\>Edit zzz.java

Program 1:

zzz.java

import   java.applet.* ;
import   java.awt .* ;
public class zzz extends  Applet
{ 
public void paint(Graphics g)
{
g.drawString("FrenchBean",10,10);
}
}

This Program ends here.!!!

We had this printed , just in case you were trying to turn pages, unable to believe that a java bean (an eight lettered word ), can be such a simple program. Then, what was all the hype about technology, we mentioned earlier ?

There is one thing every person must understand about any new technology. It always makes life easier by doing all the bad (complex) jobs for us. So to succeed in the market it has to be very simple to use. Enough philosophy for now, let's get down to work . JavaBean is just another java program which can be used as a component in a container.

An Ode to the Jars :

Bungee jumping, skiing, scuba diving and surfing the net are the hobbies of millions, the world around . But did you ever try to think why surfing the net comes last on the list of hobbies ? It 's because of the amount of time you have to wait before things come to you .

When a person goes to a site on the Net and asks for particular information, he is connected to a different site where this information is available . The required data is in the form of a html document i.e. he first connects to the server, and gets the html document. Before showing up, the application, in this case let's suppose it is an animation, might ask for some . gif files, some . class files etc. So, for retrieving each file, a special request has to be sent to the the server, and the bytes have to be transferred to the client. Moreover there is a lot of congestion on the net (The servers are busy ). So, the time taken for the transfer of the required bytes depends on the quality of the line, congestion on the server (too many clients accessing the same server) etc. Statistically speaking , even if we were to send you a message saying "Hello", 40 additional bytes of data(called as a TCP/IP header) would have to be transferred along with this message. So the gist is that each time there is a transfer of actual meaningful data from or to the client, there is also a transfer of an additional 40 bytes of data irrelevant to us.

Suppose your friend gave you a mystery novel. By the time you reached the climax, you found out that the other part of the book was missing. Anxious to read the other part , you went to your friend's place, but in vain. Your friend had left for some place and wouldn't return until the next day. You were helpless and had no other alternative, but to wait . Had you friend been thoughtful enough , he would have given you the other part also.!

Sun Microsystems has infact been thoughtful enough and hence, they introduced the concept of JARS. All the . gif files, . class files relating to a particular application are put in the jar and compressed . So when the client asks for the html program ,the server is kind enough to send you the whole jar . Now of course, at one go , you have all the information . The client does not have to ask the server, (who is always busy) again and again for the files relating to the particular program. In this way, both the server and the client save precious time. This has been done to make the life of net surfers easier. Mind you, the previous program can be called as a javabean only when put in a jar file .

Back to the zzz.java program previously written

  1. For compiling our code, give the following command
    c:jfs>javac zzz.java
  2. edit zzz.mf // The manifest file
    zzz.mf
    Manifest-Version : 1.0
    Name : zzz.class
    Java-Bean : True
  3. Now, let's see what the command jar is all about.
    It is a valid java command whose function is to put all the required files into the jar.
  4. c:jfs>jar cfm zzz.jar zzz.mf zzz.class
    This command is for including all the files of a particular application in the jar file .
    By this command we are saying, Create a File for Me called zzz.jar .
    This is followed by the manifest file. The Manifest file informs the jar that, of all the files included in it, zzz . class is the javabean. This is followed by zzz.class and all the other files connected to it .
  5. c:jfs>copy zzz.jar c:\bdk\jars
    Of course, this copies our jar file into the jars subdirectory in the bean development kit . Sun Microsystems has created a reference container for the javabeans, called the beanbox.*** The Jars subdirectory includes all the jar files which have to be implemented in the Bean Development Kit (BDK)
  6. We get out of our subdirectory and give the following command
    c:>cd \bdk\beanbox
    c:\bdk\beanbox> run>br>
Voila, and our javabean is right there at the bottom of the Beanbox toolbar.

Click and drag our javabean (zzz) into the beanbox and don't be surprised to find a "Frenchbean" on it . Now, for bringing the explicit button on to the beanbox, we first click on the button and position the mouse on the beanbox. Then we click in the Beanbox and to finally see the button. Now that we have a button and a javabean(zzz)in the beanbox , we must be satisfied but , NO!! we want something more !!We want to connect both the button and the javabean in some way.

First we click on the freshly brought button, and then look into the various options in the EDIT menuitem in the Beanbox. Now, logically speaking, we are searching for the functions which the buttonpush can do for us, hence we must checkout the action performed in the buttonpush option of the Events submenuitem.

On clicking the Action performed for a buttonpush , we find a jazzy red line with one end fixed at the button and the other end, free to move with the mouse. Now position our mouse on our zzz javabean in the beanbox and click on it. By doing this, we are actually connecting the button with our zzz javabean. As soon as we click on the zzz javabean , an Event Target Dialog (dialog box with this heading) pops up in the beanbox.. This dialog box has a big list of functions which can be assigned to our pushbutton. Of the many functions in the dialog box only one seems to be both familiar and mysterious i.e. hide so, we select it, click on OK and wait for the fun.

Now let's emulate the disappearing act of Houdini . Press the pushbutton, of course with the mouse click and see the "Frenchbean" disappear i.e. the javabean (zzz) in the beanbox disappears.

The Fundamentals of a Property :

Now, it's time for some fun. To begin with, we first look into the toolbox (beanbox frame) and select the juggler icon . We have to bring it into our beanbox , before the fun begins. The procedure is similar to obtaining the explicit button in the beanbox. There is something called as the property sheet, just beside the beanbox. Can we interact with the juggler ? This is the first question in our Querying minds.The answer is, YES WE CAN !! But first, let's take a closer look at the property sheet, which tells us that, it has a property called as the animationrate which can be changed by the user. So,what are we waiting for ? let's enjoy ourselves, by changing the value of the property and watching the juggler , juggling to our commands.(actually, when we increase the property rate, the juggling becomes slow and vice-versa ) Such properties, believe us, are fun to play with, if you are not a programmer . But what if God decided that you'll be a programmer in this life. You have two alternatives , either pray to God, to have mercy on you or accept the challenge . Take our word for it and accept the challenge of getting your fundas clear.

So, what is a property?

In short, it is a variable which can be changed by the user. Now,It is very easy to fix a certain variable i.e.initialize it in the program itself. In the program, it is easy to write that if a certain variable is greater than some value then execute some code .But it is very difficult to define a variable in such a way that it can be changed by the user and run in real time .(i.e. the effect of the change in the variable is seen in the output instantaneously. Then how do we incorporate a property which can be changed by the user?

Every property has to have a get and a set . Only two things can be done with a variable.

i) set ii) get

example :

Suppose we say ,
i = 10
By this , we are setting the variable i to 10 .
and when we have i on the right i.e.
something = i + 4
we are getting(returning) the variable i for some other purpose .Hence for i to be a variable for the user (property), we need to specify two functions , Get and the Set . The function which says set has to be given the value 10 whereas the function which says get has to return this value .

*** Actually something called as the Introspection API and the reflection API go through the code and give us a property for the set and get functions.

zzz.java

import  java.applet.* ;
import  java.awt.* ;
public class zzz  extends  Applet 
{
int aa =10 ;
public void  paint (Graphics  g)
{
g.drawString ("Great" + aa,  1,  1)  ;
}
public void setxx(int  i)
{
aa = i ;
}
public int getxx (  )
{
return aa ;
}
}

This is a javabean only after placed in a jar . Hence run the batch file that follows after editing the zzz.mf file as before .

a.bat

javac zzz.java
jar cfm zzz.jar zzz.mf zzz.class
copy zzz.jar c :\bdk\jars
cd \bdk\beanbox
run

Hey!! There, we have our property xx in the property sheet . Next to which we see a value 10 which can be changed by us. The result is seen in the beanbox in real time.

The results of the experiments with the property sheet are as follows:
when xx =10 , we see " Great 10 " in the beanbox .
and suppose it is changed to xx = 100 , we see " Great 100" in the beanbox .
Shake yourself up !!! you've done it !!and that too all by yourself.You've just written your own property . We are bubbling with confidence and oozing with enthusiasm so,let's not waste time and get down to something even more interesting.

Simulating our own function :

Previously to enact Houdini, you clicked on action performed for a push button and chose a target method ( assigned to the push button ) i.e . hide . We can actually get our own function in the Event Target Dialog . For this we make modifications in the previous program of a property.

Remove the "getxx" and the "setxx" functions from the previous program, and put the following function in their place.

public  void  abc( )
{
aa = 1000;
repaint ();
}

Now, run the a.bat file for compilation of the .java code , creation and inclusion of the files into the jar file and finally copying the jar file into the jars subdirectory.

Then we go through the whole procedure of obtaining our tool zzz and the explicit button on the beanbox and proceed to the Event Target Dialog by clicking on action performed of a buttonpush as before . The function abc ( ) that we included in our code is up on the list . To see the magic of our function , we select it and click on OK . We then click on the button and see the following change in the beanbox.

"Great 10 " to " Great 1000"

Smart Question : Why did we choose only abc ( ) and not any other name for our function, like xyz etc ?

Answer : So that, you find it up on the list, and don't have to sweat searching for your function named xyz in the Event Target Dialog .

Click here to try out some more programs on JavaBeans.


The above tutorial is a joint effort of

Mr. Vijay Mukhi
Mr. S. SriNagesh
Mr. Vinod Rasquinha


Back to Java Page

Back to the Cornucopia Page


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Mumbai 400 034, India
Tel : 91-22-496 4335 /6/7/8/9     Fax : 91-22-307 28 59
e-mail : vmukhi@giasbm01.vsnl.net.in
http://www.vijaymukhi.com