7.
Java Beans
Beans in the Market
If you go to any
marketplace 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 Java Beans are
available as well. We’ll leave all the other beans to the vegetable vendors and
cooks to figure out and talk of Java Beans in this chapter.
Before we try and
figure out what a Java Bean is, let’s explore the world of COMPONENTS and
CONTAINERS. Let’s try and get the 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, buys a
speller-checker from B and includes it in his E-MAIL program. Now B’s program
is a component, which is added to A’s program(container). Not every programmer
who wrote the speller-checker could get the sign the deal with A(container).
Only that program, which complied with, or followed all the set of rules put
forth by A got it.
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 ‘Java Beans’. They also developed a
reference container for these Java Beans to show the world the rules to be
followed while using them.
Create a subdirectory
to work in. We have named our directory jb.
Bare Minimum
Requirements for learning Java Beans
1. Windows Operating
System (95/98/NT)
2. JDK 1.1.x
c:\jb\>edit zzz.java
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 the 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 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 . Java Bean 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 some information, he is connected to the 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 being shown, the application (let’s suppose it is some animation
software), might ask for some .gif files, or 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 since the servers may be busy. Thus, 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 a lot of over head involved.
Suppose your friend
had given you a mystery novel. You start reading it and are interested. By the
time you reached the climax and the mystery situation, you find out that the
second half of the book is missing. Anxious to read the later chapters, you
visit your friend, but in vain, as he’s left for some other place and wouldn’t
be returning until later the next day. As there is no other alternative, you wait. Had you friend been thoughtful,
he would have given you the other part too!
Sun Microsystems has
been thoughtful enough and they introduced the concept of JARS. All the .class
and other files relating to a particular application are compressed and put in
the jar. So when the client asks for the html program containing the bean, the
server is kind enough to send you the whole jar. Now you have all the
information at one go. The client does not have to connect to the server again
and again for the files relating to the particular program. In this way, both
the server and the client save time. This has been done to make the life of net
surfers easier. Mind you, the previous program can be called as a Java Bean
only when put in a jar file.
To compile zzz.java ,
give the following command
c:\jb>javac zzz.java
In order to create the
manifest file, create a file with .mf extension. Here we give the filename as
zzz.mf
c:\jb>edit zzz.mf // The manifest file
zzz.mf
Manifest-Version: 1.0
Name: zzz.class
Java-Bean: True
The jar program
creates a .jar file. It is a utility provided by Sun which puts all the
required files of a particular application into the jar file.
c:\jb>jar cfm zzz.jar zzz.mf zzz.class
c:\jb>dir /w
Volume in drive C has no label.
Volume Serial Number is 1AD6-0B09
Directory of C:\jb
[.] [..] zzz.java zzz.class zzz.mf zzz.jar
4 File(s) 1,320 bytes
2 Dir(s) 545,153,024 bytes free
The cfm options to jar
file mean the following.
-c
create new archive
-f
specify archive file name
-m
include manifest information from specified manifest file
The new archive to be
created is given first, then the manifest file which contains the bean
information and lastly the .class files which belong to the application. Note
that only one class can be called a bean. The other class files that you
specify are more like supportive files that contain code.
c:\jb>copy zzz.jar c:\progra~1\bdk1.1\jars
Sun Microsystem has a
bean development kit which can be downloaded from their site. We have installed
bdk1.1 . Here they have created a reference container for Java Beans, called
the beanbox. The container looks at the jars subdirectory for all the jar files
which have to be implemented in the Bean Development Kit (BDK). Hence we copy
our .jar file into this subdirectory
Now to see our bean
work, we change to the bdk subdirectory and run a batch file viz. run.bat
c:\jb>cd c:\progra~1\bdk1.1\beanbox
c:\Program Files\BDK1.1\beanbox>run
Voila! Our Java Bean -
zzz is right there at the bottom of the Beanbox toolbar. Click and drag zzz into the beanbox container and you’ll
see a “Frenchbean” on the screen. Now, to bring the explicit button to the
beanbox, we first click on the button and position the mouse on the container
and then we click. Now that we have both a button and our Java Bean (zzz)in the
beanbox container, let’s learn how to connect both of them.
First, we click on the
freshly brought button and then look into the various options in the EDIT
menuitem. Now, logically speaking, we want to see what the buttonpush can do
for us, hence we 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 the mouse
on zzz and click on it. By doing this, we are actually connecting the button
with our zzz Java Bean. As soon as we click on the zzz Java Bean, an Event
Target Dialog (a dialog box with this heading) pops up. 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 and click on
OK.
Now let’s emulate the
disappearing act of Houdini. Press the pushbutton with a mouse click and see
the “Frenchbean” disappear i.e. the Java Bean (zzz) in the beanbox container
disappears.
The Fundamentals of a
Property :
Now, it’s time for
some fun. To begin with, we first select the juggler icon from the toolbox
(beanbox frame) and bring it into our Container. Can we interact with the
juggler? This is the first question we should ask. The answer is, YES WE CAN !!
There is a small
window named the property sheet next to the beanbox container. This window has
a property named animationrate which can be changed. By changing the value of
the property, you will see the juggler juggling either slower or faster than
before.
What is a property?
It is a variable that
can be changed by the user. Now, It is very easy to create a variable and
initialize it in the program itself. You can then have conditions and code that
are executed when the variable is greater than some value or less than a
certain value. When you change the value in the property sheet, a variable has
to be created that accepts new values at run time and shows the effect
immediately. To incorporate this feature, every variable has to have two
functions that have to be preceded by either a get or a set. Such variables are
called properties.
Only two things can be
done to a variable. You can either ‘get’ it or ‘set’ it.
example :
When i is initialised
to 10 by giving
i = 10
we are setting the
variable i to 10 .
and when i is used on the right side of the equalto
i.e.
something = i + 4, we
are getting (returning) the value stored in variable i.
The function which has
‘set’ has to be given a value whereas the function which has ‘get’ has to
return some value.
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 ;
}
}
Create a batch file
with the following commands and then
execute it
a.bat
javac zzz.java
jar cfm zzz.jar zzz.mf zzz.class
cd c:\progra~1\bdk1.1\beanbox
run
Now when you bring in
the bean into the container, you will see xx with the value 10 as a property in
the PropertySheet. You can change this value and the resultant value will then
be displayed in the Container.
When xx =10, we see “ Great 10 “ in the beanbox
and if we change it to xx = 100, we see “ Great 100” in the beanbox .
Simulating our own
function
Previously, to make
something disappear, we clicked on action performed for a push button and chose
a target method ( assigned to the push
button ) i.e . hide . We can now have our own function in the
Event Target Dialog. For this we make modifications to the previous program.
Remove the “getxx” and
the “setxx” functions from the previous program, and put the following function in the code
public void abc( )
{
aa = 1000;
repaint ();
}
Run the file a.bat.
Bring the bean in the container and then the explicit button. 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. Select it
and then click on OK. Now click on the button and see the value change on the
screen.
“Great 10 “ now
becomes “ Great 1000”
We’ve chosen names
like abc so that we can easily find them as the first entry on the list.