Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 1 Chapter 27 JavaBeans and.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 17 Creating User Interfaces.
Advertisements

Component Oriented Programming 1 Chapter 2 Theory of Components.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 42 Web Services.
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
Chapter 11 Exception Handling and Event Handling.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Event-Driven Programming
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L24 (Chapter 25) Networking.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L04 (Chapter 15) Creating.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
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.”
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
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
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 2, Week 4 Design by Contract, Java Exceptions, Eventing, Finding the Classes,
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
TM Introduction to JavaBeans™ Dimitrios Psarros Questra Consulting (716) x225.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
1 Chapter 21 JavaBeans, Bean Events, and MVC Architecture.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 43 Remote Method Invocation.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Object Oriented Programming.  Interface  Event Handling.
Java Beans. Definitions A reusable software component that can be manipulated visually in a ‘builder tool’. (from JavaBean Specification) The JavaBeans.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
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,
Chapter 20 Concepts for Object-Oriented Databases Copyright © 2004 Pearson Education, Inc.
Java Beans - Basics CIS 421 Web-based Java Programming.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 29 Remote Method.
Chapter 32 JavaBeans and Bean Events
CompSci 230 S Programming Techniques
Chapter 36 JavaBeans and Bean Events
Java Beans Sagun Dhakhwa.
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chapter 21 JavaBeans, Bean Events, and MVC Architecture
Exception Handling and Event Handling
Component-Based Software Engineering
CMSC 202 Exceptions.
Presentation transcript:

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and Bean Events

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Objectives F To know what a JavaBeans component is (§27.2). F To discover the similarities and differences between beans and regular objects (§27.2). F To understand JavaBeans properties and naming patterns (§27.3). F To review the Java event delegation model (§27.4). F To create custom event classes and listener interfaces (§27.5 optional). F To develop source components using event sets from the Java API or custom event sets (§27.6).

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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 JBuilder and NetBeans.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved JavaBeans Properties and Naming Patterns F The get method is named get (), which takes no parameters and returns an object of the type identical to the property type. F For a property of boolean type, the get method should be named is (), which returns a boolean value. F The set method should be named set (newValue), which takes a single parameter identical to the property type and returns void.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved The Event Delegation Model Figure 14.2

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Predefined Event Pairs (Event Classes and Listener Interface) Examples: ActionEvent/ActionListener AdjustmentEvent/AdjustmentListener

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Examples of Event Pairs

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Source Components The source component detects events and processes the events by invoking the event listeners' handler.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Listener Components The listener is registered with the source, and the source invokes the listener's handler to process the event.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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 Listener for the corresponding event class named.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Creating a Custom Event Set Problem: This example creates a custom event named TickEvent for describing tick events, and its corresponding listener interface TickListener for defining a tick handler. TickEvent TickListener

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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 Listener( Listener l) throws TooManyListenersException;

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved 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 Listener( Listener l) The naming pattern for removing a listener (either unicast or multicast) is: public void remove Listener( 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.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Creating a Source Component Problem: This example creates a custom source component that generates a tick event at every specified time interval in milliseconds. Create a custom source component that is capable of generating a tick event at a variant time interval. The component contains the properties tickCount, tickInterval, maxInterval, minInterval, and step. The component adjusts the tickInterval by adding step to it after a tick event occurs. If step is 0, tickInterval is unchanged. If step > 0, tickInterval is increased. If step maxInterval or tickInterval < minInterval, the component will no longer generate tick events. NOTE: You learned to use javax.swing.Timer to control the animation in Chapter 19, “Multithreading.” The Timer class generates a timer at a fixed time interval. This new component can generate a tick event at a variant time interval as well as a fixed time interval.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Creating a Source Component Tick

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Using the TickEvent Class Problem: Write a program that displays a moving message. Solution: You can write the code using the Thread.sleep(millis) method or the Timer class to control the animation (See Exercise 19.11). This example uses the Tick class to display the message periodically. DisplayMovingMessage Run

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Interaction Between Source and Listener Components The listener messagePanel is registered with the source tick, and the source invokes the listener's handler handleTick to process the event.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Working with Existing Event Sets TickEvent and TickListener is a new event pair. Most of the time you don't need to create your own event pairs unless you want to encapsulate information not available in the existing event classes, as in the case of the TickEvent class that contains tick count and tick interval. If you don't need the tick count and tick interval contained in a tick event. There is no need to create a TickEvent class; instead you can use java.awt.ActionEvent and let the Tick class generate an ActionEvent instance when a tick event occurs.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Example: Developing a Source Component Using Existing Event Sets Problem: This example presents a new component that generates an ActionEvent when a tick event occurs rather than using a TickEvent. Use this new component to rewrite the preceding example to display a moving message. TickUsingActionEvent Run DisplayingMessageUsingActionEvent

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Interaction Between Source and Listener Components The listener messagePanel is registered with the source tick, and the source invokes the listener's handler actionPerformed to process the event.

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Note to the Instructor You may cover Chapter 30 now.