Presentation is loading. Please wait.

Presentation is loading. Please wait.

98-09-281 JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.

Similar presentations


Presentation on theme: "98-09-281 JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science."— Presentation transcript:

1 98-09-281 JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science

2 98-09-28Computer 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)

3 98-09-28Computer Science, University of Karlstad3 Example for Builder

4 98-09-28Computer 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

5 98-09-28Computer Science, University of Karlstad5 Example Interoperability

6 98-09-28Computer Science, University of Karlstad6 The Converter in Delphi

7 98-09-28Computer 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

8 98-09-28Computer Science, University of Karlstad8 Basic Concepts A Java Bean supports l Introspection l Customization l Properties l Events l Persistence

9 98-09-28Computer 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

10 98-09-28Computer 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

11 98-09-28Computer 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

12 98-09-28Computer Science, University of Karlstad12 The Bean Development Kit Available for free at http://www.javasoft.com/products/javabeans/software/ http://www.javasoft.com/products/javabeans/software/ l Reference implementation of a builder application to test own Java Beans l Allows customization of Beans and simple linking of Beans with adapters

13 98-09-28Computer Science, University of Karlstad13 The Beanbox

14 98-09-28Computer Science, University of Karlstad14 Example ProgressBean TimerBean ButtonBean The TimerBean calls the ProgressBar in an editable intervall after being started by the Button.

15 98-09-28Computer Science, University of Karlstad15 Class Diagram

16 98-09-28Computer Science, University of Karlstad16 Example Interaction Diagram

17 98-09-28Computer 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

18 98-09-28Computer 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

19 98-09-28Computer 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; }

20 98-09-28Computer 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)); }

21 98-09-28Computer 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

22 98-09-28Computer 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); }

23 98-09-28Computer 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); }


Download ppt "98-09-281 JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science."

Similar presentations


Ads by Google