JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine

Slides:



Advertisements
Similar presentations
Copyright © 2001 Qusay H. Mahmoud JavaBeans An introduction to component-based development in general Introduction to JavaBeans – Java components – client-side.
Advertisements

OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
JavaBeans introduction Klaus-Peter Heidrich for the University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of Creating Eclipse plug-ins.
By Dr. Jiang B. Liu 12. The Java Beans. Java Beans n JavaBeans is a portable, platform-independent software component model written in Java. It enables.
JAVA BEANS By Madhuri Kakumanu. What is a Java Bean? “ A Java Bean is a reusable software component that can be visually manipulated in builder tools.”
Object-Oriented Analysis and Design
JAVA BEANS (Unit-V) K.Phani Sirisha.
Introducing JavaBeans Lesson 2A / Slide 1 of 30 JDBC and JavaBeans Pre-assessment Questions 1.Which of the given symbols is used as a placeholder for PreparedStatement.
Introducing JavaBeans Identify the features of a JavaBean Create and add a bean to an application Manipulate bean properties using accessor and mutator.
Java Beans.  Java offers software component development through java Beans  Java Beans are based on a software component model for java.  The model.
Java Beans Component Technology Integrated, Visual Development Environments Reusable Platform-Independent Modular.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Java Beans.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Chapter 10 Introduction to Components. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
Last update October 18, 2004 Advanced Programming 2004 Java Beans.
Applets Life Cycle Drawing and Event Handling Methods for UI Components Applet Capabilities Example.
Introduction to Java Beans by Anders Børjesson. Introduction to JavaBeans2 JavaBeans components JavaBeans are the components in the Java environment Many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
SWE 316: Software Design and Architecture Objectives Lecture # 18 Introduction to Components SWE 316: Software Design and Architecture To learn:  benefits.
Components Components are specialized self contained Software entities that can be replicated, Customized and inserted into applications. Components come.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Introduction to Java Beans CIS 421 Web-based Java Programming.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Object Oriented Programming.  Interface  Event Handling.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Advanced Java Session 4 New York University School of Continuing and Professional Studies.
JavaBean Component Java bean is a reusable software component that can be manipulated visually in a builder tool Graphic bean and Non-graphic bean Javabean.
Introduction to Java Beans From Anders Børjesson.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Beans THETOPPERSWAY.COM. Contents What is a Java Bean? Advantages Properties Bean Developers Kit (BDK) What makes Bean possible? Support for Java.
Java Beans - Basics CIS 421 Web-based Java Programming.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Chapter 32 JavaBeans and Bean Events
GUIs and Events Rick Mercer.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 36 JavaBeans and Bean Events
Javabeans for dummies.
Aum Amriteshwaryai Namah
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
A First Look at GUI Applications
Java Beans Sagun Dhakhwa.
Chapter Eleven Handling Events.
Programming in Java Event Handling
Ellen Walker Hiram College
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Introduction to Computing Using Java
Web Development Using ASP .NET
Pre-assessment Questions
Knowledge Byte In this section, you will learn about:
Java Programming: Advanced Topics
Presentation transcript:

JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine ICS 123, Spring 2002 University of California, Irvine JavaBeans* ICS 123 Richard N. Taylor & Eric M. Dashofy UC Irvine http://www.isr.uci.edu/classes/ics123s02/ *with the usual thanks to David Rosenblum Topic 12, JavaBeans

What Is JavaBeans? (One of) Sun Microsystems’s entry in the component sweepstakes Currently realized in Beans Development Kit (BDK) 1.1 spec (1999) Component model for Java Platform-independent Language-dependent Implemented as language extensions Additional classes and interfaces in the Java Class Library Basically a design pattern Based on standard naming conventions The naming conventions allow beanboxes and other beans to dynamically discover a bean’s capabilities But naming conventions have well-known problems Detailed documentation is available at http://java.sun.com/products/javabeans/

ICS 123, Spring 2002 University of California, Irvine Three Levels of Use Building applications from beans written by bean developers Developing individual beans Developing “beanboxes” and other tools for graphically manipulating beans A more neutral term for such tools is “sandboxes” Topic 12, JavaBeans

What Is a Bean? A bean is a Java object Any Java class can be a bean Exports properties, events and methods Interface conforms to certain naming rules Conforms to certain packaging rules Usually (but not always) has a visual form for manipulation in beanboxes Supported by package java.beans Any Java class can be a bean No predefined Bean class Many AWT components are beans

ICS 123, Spring 2002 University of California, Irvine A Bare-Bones Bean public class Airplane extends Canvas { public Airplane() { setBackground(Color.white); // Plus other visual settings and setForeground(Color.blue); // other initialization } public Dimension getPreferredSize() { return new Dimension(50,50); // Or whatever is appropriate } public Dimension getMinimumSize() { return getPreferredSize(); // Or return an actual Dimension } } Canvas is subclassed to make the bean a GUI object having its own window Can subclass Component instead to create a “lightweight component” without its own window Topic 12, JavaBeans

Elements of JavaBeans (I) Events signal a bean’s response to changes in its properties and other phenomena Properties represent attributes of a bean BeanInfo customizes the information about a bean that is made available within a beanbox

Elements of JavaBeans (II) Customizers allow customization of a bean’s appearance and behavior in beanboxes PropertyEditors define custom editors for a specific property JAR files are archive files containing all the artifacts related to a bean Manifest files describe the content of JAR files

Events Java events are used heavily in JavaBeans An event in Java is an object Events are instances of (subclasses of) java.util.EventObject An event is sent by an object to a specific object The identity of the sender is associated with the event An event is sent as an argument in a method call An object that generates events must keep track of its own listeners

So What’s So Special About Java Events? How do they differ from regular method calls? They define a protocol for asynchronous interaction They are generated by many predefined Java components They are the primary mechanism for wiring interoperation paths between beans But there’s still something missing ...

Review: Implicit Invocation ICS 123, Spring 2002 University of California, Irvine Review: Implicit Invocation Advantages Allows for decoupling and autonomy of components Java events require tight coupling Enhances reuse and evolution Easy to introduce new components without affecting existing ones Disadvantages Components announcing events have no guarantee of getting a response Components announcing events have no control over the order of responses Event abstraction does not cleanly lend itself to data exchange Difficult to reason about the behavior of an announcing component independently of the components that register for its events Topic 12, JavaBeans

Listeners and Adapters Listeners are interfaces They define methods that an object must implement in order to receive event notifications Listeners add and remove themselves (i.e., register and unregister) with the source of events they’re interested in Event sources must provide methods to add and remove listeners Adapters are classes They are bare-bones implementations of listener interfaces They provide a convenient way of quickly creating event listeners They are usually subclassed to override a single listener method of interest

Predefined Events in Java ActionEvent: high-level user event AdjustmentEvent: event from an Adjustable object (such as a scrollbar) ComponentEvent: event from a GUI Component object Container, Focus, Input (Key or Mouse), Paint, Window ItemEvent: event from an ItemSelectable object TextEvent: event from the editing of a TextComponent Many of these have associated Adapters and/or Listeners

Event Handling Scenario (involving ActionEvents) ActionEvent Listener ActionEventSource ActionEvent Listener addActionListener() actionPerformed(ae) addActionListener() actionPerformed(ae) actionPerformed(ae) removeActionListener() actionPerformed(ae)

Action Event Programming Example public class Button extends Component { // defined in java.awt public synchronized void addActionListener(ActionListener l) { ... } protected void processEvent(ActionEvent ae) { // called internally ... l.ActionPerformed(ae); } } public class ButtonListener implements ActionListener { Button button; public ButtonListener() { button = new Button("Press Me"); button.addActionListener(this); } public void ActionPerformed(ActionEvent buttonClick) { ... } }

Properties A property of a bean is a public attribute, accessed via Get and Set methods Read-only properties have only a Get method Write-only properties have only a Set method Read/Write properties have both

Kinds of Properties (I) Simple properties: a single value P of some type T public T getP(); public void setP(T newP); public boolean isP(); // same as getP when T is boolean Indexed properties: an array of values public T[] getP(); public void setP(T[] newP); public T getP(int index); public void setP(int index, T newP); Not supported in BDK 1.0 How does a beanbox know that something is a simple or indexed property?

Example Simple Properties ICS 123, Spring 2002 University of California, Irvine Example Simple Properties public class Airplane extends Label { protected String serialNumber = "N173UA"; public String getSerialNumber() { return serialNumber; } public Airplane() { setText(getSerialNumber()); } protected int flightNumber = 0; public int getFlightNumber() { return flightNumber; } public void setFlightNumber(int n) { flightNumber = n; } } FlightNumber is a read/write property SerialNumber is a read-only property Attributes storing the property values are made protected Topic 12, JavaBeans

Kinds of Properties (II) Bound properties: signal a PropertyChange event when changed One event listener list for all bound properties Supported by classes and interfaces in package java.beans public class PropertyChangeEvent extends java.util.EventObject public interface PropertyChangeListener extends EventListener class PropertyChangeSupport implements Serializable public void addPropertyChangeListener(PropertyChangeListener l); public void removePropertyChangeListener(PropertyChangeListener l); How does a beanbox know that something is a bound property?

Example Bound Property import java.beans.* // Needed for PropertyChange stuff public class Airplane extends Label { protected int altitude = 0; protected PropertyChangeSupport changes = new PropertyChangeSupport(this); public int getAltitude() { return altitude; } public void setAltitude(int a) { int oldAltitude = altitude; altitude = a; changes.firePropertyChange("Altitude", new Integer(oldAltitude), new Integer(a)); } public void addPropertyChangeListener(PropertyChangeListener l) { changes.addPropertyChangeListener(l); } public void removePropertyChangeListener(PropertyChangeListener l) { changes.removePropertyChangeListener(l); } }

Kinds of Properties (III) Constrained properties: changes can be vetoed by other objects One event listener list for all constrained properties Supported by classes and interfaces in package java.beans exception PropertyVetoException public interface VetoableChangeListener extends EventListener class VetoableChangeSupport implements Serializable public void setP(T newP) throws PropertyVetoException; public void addVetoableChangeListener(VetoableChangeListener l); public void removeVetoableChangeListener(VetoableChangeListener l); Usually also implemented as bound properties Bound property listeners also get notified of changes to constrained properties Only registered vetoers are given the opportunity to veto a change

Example Constrained Property public class Airplane extends Label { protected int heading = 0; protected VetoableChangeSupport vetoes = new VetoableChangeSupport(this); public int getHeading() { return heading; } public void setHeading(int h) throws PropertyVetoException { int oldHeading = heading; vetoes.fireVetoableChange("Heading", new Integer(oldHeading), new Integer(h)); // ***** heading = h; changes.firePropertyChange("Heading", new Integer(oldHeading), new Integer(h)); } public void addVetoableChangeListener(VetoableChangeListener l) { vetoes.addVetoableChangeListener(l); } public void removeVetoableChangeListener(VetoableChangeListener l) { vetoes.removeVetoableChangeListener(l); } } This is also a bound property (using the setup for Altitude) PropertyVetoException would be raised within call at *****

JAR Files JAR archives contain files related to a bean .class files .ser files (serialized Beans) .html files image files audio files Manifest file Manifest entries can include several fields, including an optional indication of whether or not a file is a bean In the absence of a manifest file, all .class and .ser files are treated as beans

Packaging the Airplane Bean Create a MANIFEST file Name: Airplane.class Java-Bean: True Name: AirplaneBeanInfo.class Java-Bean: False Name: Airplane.gif Create the JAR file jar cfm Airplane.jar MANIFEST Airplane.class \ AirplaneBeanInfo.class Airplane.gif The jar command creates a file called META-INF/MANIFEST in the .jar file it creates, using the information contained in MANIFEST above

BDK Directory Structure <root-directory> GNUmakefile beanbox docs jars … Run beanbox from beanbox directory run.sh in UNIX, run.bat in Windows Beanbox looks in jars directory for .jar files Look at GNUmakefile for ideas on how to automate creation of beans Lots of documentation in docs directory

The BDK Beanbox A simple beanbox for graphically composing beans and testing bean interactions Three windows Beanbox: A canvas where beans are placed and wired together Toolbox: A palette of available beans Property sheet: Properties of the selected bean The beanbox finds read-only and write-only properties for a bean but by default doesn’t display them in the property sheet for the bean

The BDK Beanbox with the Airplane Bean Property Sheet Beanbox Toolbox

Additional BDK Beanbox Expectations Each bean class needs a zero-argument constructor method The beanbox calls this method when you drop a bean in the beanbox Helpful to define a package for all classes related to a bean

Wiring Beans Together in the BDK Beanbox Beanbox customizes Edit menu according to capabilities of currently selected bean Wiring events Event from generated bean passed as argument to method of target bean Predefined events for the bean’s class Component, container, key, mouse, focus, mouseMotion events PropertyChange events VetoableChange events Any other events generated by the bean Wiring bound properties Change in bound property in one bean can be bound to a property in another bean

Wiring Up Two Beans (I) An Airplane bean and a Juggler bean Airplane is currently selected

Wiring Up Two Beans (II) Select propertyChange event in Airplane bean

Wiring Up Two Beans (III) Wire the propertyChange event to the Juggler

Wiring Up Two Beans (IV) Have the juggler’s startJuggling method called upon receiving the Airplane’s propertyChange event

Wiring Up Two Beans (V) Juggler is now selected Select mouseClicked event in Juggler bean

Wiring Up Two Beans (VI) Wire the Juggler’s mouseClicked event to itself

Wiring Up Two Beans (VII) Have the juggler’s stopJuggling method called upon receiving its own mouseClicked event

Wiring Up Two Beans (VIII) Everything is now wired up Click on the Juggler to stop its juggling Change the altitude of the airplane to start the Juggler juggling