JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science
Computer Science, University of Karlstad2 Introduction What is a Java Bean? l „A Java Bean is a reusable software component that can be visually manipulated in builder tools.“ l Java Beans are Java classes that conform to the Java Beans specification, can be visible (button, slider) or invisible (network component)
Computer Science, University of Karlstad3 Example for Builder
Computer Science, University of Karlstad4 Motivation l Build an application out of prebuild parts like in industrial production of for example cars or computers l Cross platform component model l Interoperability (ActiveX, OpenDoc...) l Constructing instead of coding l Visual building of applications
Computer Science, University of Karlstad5 Example Interoperability
Computer Science, University of Karlstad6 The Converter in Delphi
Computer Science, University of Karlstad7 How is it done? l A Bean is delivered in binary form l It exposes its attributes (methods, instance variables, properties, and interfaces) through a default mechanism included in the Java API or explicit Information Classes l Bridges translate the attributes to other Component Models
Computer Science, University of Karlstad8 Basic Concepts A Java Bean supports l Introspection l Customization l Properties l Events l Persistence
Computer Science, University of Karlstad9 Requirements for a Bean A Bean is represented by one ore more Java classes which follow the Java Beans specifications, it must not inherit from any special class A JavaBean must l Have a default Constructor without no arguments l be Customizable allowing access to its internal state l be Serializable, must be able to save its state in a stream It should l be Packaged in JAR-File all Classes, Pictures, Documentation... in one file l provide BeanInfo and Customizer Classes
Computer Science, University of Karlstad10 Design vs. Runtime A Java Bean has two lives: l In the design environment, where it should provide design information to the application builder to let the user customize its behaviour and appearance l In the application to do the task it was build for
Computer Science, University of Karlstad11 Features of a Java Bean A Java Bean has certain attributes l Properties To represent the internal state l Methods To be called from outside l Events To inform about changes
Computer Science, University of Karlstad12 The Bean Development Kit Available for free at l Reference implementation of a builder application to test own Java Beans l Allows customization of Beans and simple linking of Beans with adapters
Computer Science, University of Karlstad13 The Beanbox
Computer Science, University of Karlstad14 Example ProgressBean TimerBean ButtonBean The TimerBean calls the ProgressBar in an editable intervall after being started by the Button.
Computer Science, University of Karlstad15 Class Diagram
Computer Science, University of Karlstad16 Example Interaction Diagram
Computer Science, University of Karlstad17 Properties represent the internal state of a component control behaviour or visual representation can be read or written the process of setting up properties at design- or runtime is called Customization
Computer Science, University of Karlstad18 Properties Properties can be: l Simple (simple Java type or Class) l Indexed (an array of the above) l Bound an event is created when the property changes to inform the attached Listeners l Constrained like Bound, but the Listeners can revert the change
Computer Science, University of Karlstad19 Property Example A Property is defined by two accessor methods (setter and getter) allowing other classes or the Builder to manipulate their state public void setTimeout (int t) { timeout = t; } public int getTimeout () { return timeout; }
Computer Science, University of Karlstad20 Bound Properties Example protected PropertyChangeSupport listeners = new PropertyChangeSupport(this); public void addPropertyChangeListener (PropertyChangeListener l) { listeners.addPropertyChangeListener(l); } public void removePropertyChangeListener (PropertyChangeListener l) { listeners.removePropertyChangeListener(l); } public synchronized void setValue(int v) { Integer oldValue = new Integer(value); value = v; repaint(); listeners.firePropertyChange("value", oldValue, new Integer(value)); }
Computer Science, University of Karlstad21 Events l Mechanism to plug beans together l Event is a notification between source and one or more listeners l Listener registers itself in being interested in events l Beans may be plugged together by adapters generated by builder tool
Computer Science, University of Karlstad22 TimerEvent public class TimerEvent extends EventObject { long time; public TimerEvent(Object source, long time) { super(source); this.time = time; } public long getTime() { return time; } public interface TimerListener extends EventListener { public abstract void timedOut(TimerEvent te); }
Computer Science, University of Karlstad23 Handling Listeners protected Vector listeners = new Vector (); public void addTimerListener (TimerListener listener) { listeners.addElement (listener); } public void removeTimerListener (TimerListener listener) { listeners.removeElement (listener); } protected void fireTimeOut() { TimerEvent event = new TimerEvent (this, System.currentTimeMillis()); Vector listeners = (Vector) this.listeners.clone (); for (int i = 0; i < listeners.size (); ++ i) ((TimerListener) listeners.elementAt (i)).timedOut (event); }