Chapter 36 JavaBeans and Bean Events

Slides:



Advertisements
Similar presentations
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Event Handling --
Advertisements

Java Beans & Serialization CS-328 Dick Steflik. Java Beans Java based component technology –originally developed to provide java with a component technology.
Event Handling Events and Listeners Timers and Animation.
Chapter 11 Exception Handling and Event Handling.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Object-Oriented Analysis and Design
Internet Software Development The Java Event Model Lecture 5.
Component-Based Software Engineering Introduction to Java Beans Paul Krause and Sotiris Moschoyiannis.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Java Beans.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Jaeki Song ISQS6337 JAVA Lecture 16 Other Issues in Java.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 2, Week 4 Design by Contract, Java Exceptions, Eventing, Finding the Classes,
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
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.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Systems Analysis and Design in a Changing World, 3rd Edition
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Java Programming: Advanced Topics 1 JavaBeans Chapter 8.
Java Bean Definition “A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
1 Chapter 21 JavaBeans, Bean Events, and MVC Architecture.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Observer design pattern A closer look at INotifyPropertyChanged, INotifyPropertyChanging and ObservableCollection Observer design pattern1.
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.
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Introduction to Java Beans From Anders Børjesson.
Introduction to Software Components: the JavaBeans specs Babak Esfandiari.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Lorenz: Visitor Beans: An Aspect-Oriented Pattern Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
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 - Basics CIS 421 Web-based Java Programming.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
Chapter 32 JavaBeans and Bean Events
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
Observer Pattern Context:
Web Design & Development Lecture 11
A First Look at GUI Applications
Remote Method Invocation
Java Beans Sagun Dhakhwa.
Delegates and Events 14: Delegates and Events
Review: Java GUI Programming
Chapter Eleven Handling Events.
Programming in Java Event Handling
Ellen Walker Hiram College
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
GUI Programming III: Events
EE 422C Java FX.
C# Event Processing Model
Introduction to Computing Using Java
Chapter 21 JavaBeans, Bean Events, and MVC Architecture
Chapter 40 Remote Method Invocation
Exception Handling and Event Handling
Chapter 46 Remote Method Invocation
Pre-assessment Questions
Chapter 46 Remote Method Invocation
Component-Based Software Engineering
Java Programming: Advanced Topics
Knowledge Byte In this section, you will learn about:
Chapter 15 Event-Driven Programming and Animations Part 1
CSC 243 – Java Programming, Fall, 2008
Presentation transcript:

Chapter 36 JavaBeans and Bean Events

Objectives To describe what a JavaBeans component is (§36.2). To explain the similarities and differences between beans and regular objects (§36.2). To develop JavaBeans components that follow the naming patterns (§36.3). To review the Java event delegation model (§36.4). To create custom event classes and listener interfaces (§36.5). To develop source components using event sets from the Java API or custom event sets (§36.6).

What is JavaBean? A JavaBeans component is a serializable public class with a public no-arg constructor. Every GUI class is a JavaBeans component, because (1) it is a public class; (2) it has a public no-arg constructor; (3) It is an extension of java.awt.Component, which implements java.io.Serializable.

Why JavaBeans? The JavaBeans technology was developed to enable the programmers to rapidly build applications by assembling objects and test them during design time, thus making reuse of the software more productive. JavaBeans is a software component architecture that extends the power of the Java language by enabling well- formed objects to be manipulated visually at design time in a pure Java builder tool, such as Eclipse, JBuilder and NetBeans.

JavaBeans and Builder Tools NOTE: This chapter does not require that you use any builder tools. If you are interested to use JavaBeans in rapid Java application development using JBuilder or Sun ONE Studio, please refer to Supplement I, “Rapid Java Application Development Using JBuilder” or Supplement J, “Rapid Java Application Development Using Sun ONE Studio,” on the companion Website.

JavaBeans Properties and Naming Patterns The get method is named get<PropertyName>(), which takes no parameters and returns an object of the type identical to the property type. For a property of boolean type, the get method should be named is<PropertyName>(), which returns a boolean value. The set method should be named set<PropertyName>(newValue), which takes a single parameter identical to the property type and returns void.

Properties and Data Fields Properties describe the state of the bean. Naturally, data fields are used to store properties. However, a bean property is not necessarily a data field. For example, in the MessagePanel class in Example 12.5 in Chapter 13, you may create a new property named messageLength that represents the number of the characters in message. The get method for the property may be defined as follows:   public int getMessageLength() { return message.length(); } NOTE: A property may be read-only with a get method but no set method, or write-only with a set method but no get method.

Bean Events A bean may communicate with other beans. The Java event delegation model provides the foundation for beans to send, receive, and handle events. When something happens to a bean, such as a mouse click on a javax.swing.JButton bean, an event object is created to encapsulate information pertaining to the event. The bean passes the event object to the interested beans for the event to be processed. Events are typically generated by Java GUI components, such as javax.swing.JButton, but are not limited to GUI components. This section introduces the development of custom events and the beans that can generate events.

The Event Delegation Model Figure 15.3

Predefined Event Pairs (Event Classes and Listener Interface) Examples: ActionEvent/ActionListener AdjustmentEvent/AdjustmentListener

Examples of Event Pairs

Source Components The source component detects events and processes the events by invoking the event listeners' handler.

Listener Components The listener is registered with the source, and the source invokes the listener's handler to process the event.

Creating Custom Source Components Unicast Registration Methods: A source component must have the appropriate registration and deregistration methods for adding and removing listeners. Events can be unicasted (only one object is notified of the event) or multicasted (each object in a list of listeners is notified of the event). The naming pattern for adding a unicast listener is public void add<Event>Listener(<Event>Listener l) throws TooManyListenersException;

Creating Custom Source Components Multicast Registration Methods: The naming pattern for adding a multicast listener is the same, except that it does not throw the TooManyListenersException. public void add<Event>Listener(<Event>Listener l) The naming pattern for removing a listener (either unicast or multicast) is: public void remove<Event>Listener(<Event>Listener l) A source component contains the code that creates an event object and passes it to the listening components by calling a method in the listener's event listener interface. You may use a standard Java event class like ActionEvent to create event objects or may define your own event classes if necessary.

Example: Creating a Source Component CourseWithActionEvent TestCourseWithActionEvent TestCourseWithActionEvent

Interaction Between Source and Listener Components The listener is registered with the source course, and the source invokes the listener's handler actionPerformed to process the event.

Example: Creating a Custom Event Set Problem: This example creates a custom event named EnrollmentEvent for describing enrollment events, and its corresponding listener interface EnrollmentListener for defining an enrollmentExceeded handler. EnrollmentEvent EnrollmentListener

Creating Custom Event Pairs You have already used event sets (e.g., ActionEvent/ActionListener) and event source components (JButton) in Java GUI programming. You can create your own event sets and source components. A custom event class must extend java.util.EventObject or a subclass of java.util.EventObject. Additionally, it may provide constructors to create events, data members and methods to describe the event. A custom event listener interface must extend java.util.EventListener or a subinterface of java.util.EventListener, and define the signature of the handlers for the event. By convention, the listener interface should be named <Event>Listener for the corresponding event class named <Event>.

Example: Source Component Using Custom Event Pairs CourseWithEnrollmentEvent TestCourseWithEnrollmentEvent TestCourseWithEnrollmentEvent

Interaction Between Source and Listener Components The listener is registered with the source course, and the source invokes the listener's handler enrollmentExceeded to process the event.

Note to the Instructor You may cover Chapter 36, “MVC and Swing Models,” now.