Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
OurBean calls the actionPerformed method of classes that have been added as actionListeners.
An instance can be added to a java.util.Vector for that purpose :
Naturally, there's also a method for removing a listener.
When the bean is clicked, it traverses the list of listeners and calls each listener's actionPerformed method .
First it creates an ActionEvent.
( OurBean.this because we don't want to pass the MA instance's reference as event source )
And locks the Vector, als, for itself in this block, so meanwhile other threads cannot change the Vector's contents ! ( More on this in the Threads lessons )
Because nextElement returns a reference of type Object, it has to be cast to ActionListener,....
so we can call actionPerformed :
The preferred way to deploy beans is in a package, zipped into a .jar file.
There has to be a manifest file inside the jar file in a directory META-INF, called MANIFEST.MF .
Your builder tool will probably have a means to create a .jar file and generate a manifest file.
On the other hand, you could create it with the SDK tool : jar.exe .
From the directory that contains the ourbeans directory :
ANY class containing set/get-methods+data, add/remove....Listener methods, implementing Serializable, can be used as a bean.
We've chosen Canvas, because we wanted to use a visual bean.
When there are many listeners, the approach shown is probably not okay, because the Vector stays locked.
In that case it's better to make a clone of the Vector and use that to deal with the listeners, keeping the original Vecor unoccupied.
Vector clohn = als.clone( ) ;
Vector's initial size is for 5 elements, but it can grow by 2 elements each time, if necessary.
Transient means : will not be saved or restored, which would be of no use, since listeners (un)register during run-time.