1 / 47

Chapter 21 JavaBeans, Bean Events, and MVC Architecture

Chapter 21 JavaBeans, Bean Events, and MVC Architecture. Objectives. · To know what a JavaBeans component is (§21.2). · To discover the similarities and differences between beans and regular objects (§21.2). · To understand JavaBeans properties and naming patterns (§21.3).

anahid
Télécharger la présentation

Chapter 21 JavaBeans, Bean Events, and MVC Architecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 21 JavaBeans, Bean Events, and MVC Architecture

  2. Objectives ·To know what a JavaBeans component is (§21.2). ·To discover the similarities and differences between beans and regular objects (§21.2). ·To understand JavaBeans properties and naming patterns (§21.3). ·To review the Java event delegation model (§21.4). ·To create a new event class and listener interface (§21.5). ·To develop source components using new event sets or existing event sets (§21.6). ·To utilize existing events for creating source components (§21.7). ·To distinguish standard adapters, inner classes, and anonymous classes (§21.8). ·To use the model-view-controller approach to separate data and logic from the presentation of data (§21.9). ·To implement the model-view-controller components using the JavaBeans event model (§21.9).

  3. 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.

  4. 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, Sun ONE Studio 4, WebGain Café, or IBM Visual Age for Java.

  5. 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.

  6. 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.

  7. 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 12, 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.

  8. 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 that encapsulates 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.

  9. The Event Delegation Model Figure 12.2

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

  11. Examples of Event Pairs

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

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

  14. 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>.

  15. Example 21.1 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

  16. 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;

  17. 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.

  18. Example 21.2 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 < 0, tickInterval is decreased. If tickInterval > 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.

  19. Example 21.2 Creating a Source Component Tick

  20. Example 21.3 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

  21. 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.

  22. 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.

  23. Example 21.4 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. DisplayingMessageUsingActionEvent TickUsingActionEvent Run

  24. 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.

  25. Event Adapters The Java event model shown in Figure 12.2 is flexible, allowing modifications and variations. One useful variation of the model is the addition of adapters.

  26. Event Adapters The Java event model shown in Figure 12.2 is flexible, allowing modifications and variations. One useful variation of the model is the addition of adapters.

  27. Benefits of Using Event Adapters 1. If you need to hook a source to an existing class that cannot be modified or extended, you can use an adapter as a listener to the source and register it with the source. When an event occurs, the source notifies the adapter, which then invokes the methods in the class. 2. You can use an adapter to place all the event notifications from the source in a queue so as to allow the source object to resume execution without blocking. This is particularly useful in a distributed environment, where the adaptee object may be busy or not available when the event occurs. 3. You can create a generic adapter to listen for all types of events from all sources, then deliver them to one or multiple adaptees. The generic adapter can be used as a filter for the events before they are delegated out to the adaptees. You can apply business rules and logic of all kinds to the delivery of events and sort them by priority.

  28. Convenience Listener Adapter An adapter usually extends a convenience listener adapter. A convenience listener adapter is a support class that provides default implementations for all the methods in the listener interface. The default implementation is usually an empty body. Java provides convenience listener adapters for every AWT listener interface except the ActionListener. A convenience listener adapter is named XAdapter for XListener. For example, MouseAdapter is a standard listener adapter for MouseListener. A listener interface may contain many methods for handling various types of actions of an event. For example, MouseListener contains mouseClicked, mousePressed, mouseReleased, mouseEntered, and mouseExited. The convenience listener adapter is convenient because a listener class may simply extend the adapter and implement only the method for the intended type of action instead of all the methods of the listener interface.

  29. Standard Adapters A standard adapter is a named class that extends a convenience listener adapter or implements a listener interface. The following example demonstrates the use of standard adapters. Example 21.5 Handling Events Using Standard Adapters Problem: Rewrite the preceding example with two new features: (1) The message freezes when the mouse button is pressed on the message panel, and moves the mouse button is released; (2) Use standard adapters. StandardAdapterDemo Run

  30. Source Component, Adapter, and Adaptee Interactions

  31. Inner Class Adapters Standard adapters can be shortened using inner classes. Here is an example of rewriting the preceding example using inner classes. The code related to inner classes is highlighted. InnerClassAdapterDemo Run

  32. Anonymous Inner Class Adapters Inner classes make programs simple and concise. As you can see, the new class is shorter and leaner. Inner class adapters can be further shortened using anonymous inner classes. An anonymous inner class is an inner class without a name. It combines declaring an inner class and creating an instance of the class in one step. An anonymous inner class is declared as follows:  new SuperClassName/InterfaceName() { // Implement or override methods in superclass or interface // Other methods if necessary }

  33. Anonymous Inner Class Adapters • Since an anonymous inner class is a special kind of inner class, it is treated like an inner class in many ways. In addition, it has the following features: • An anonymous inner class must always extend a superclass or implement an interface, but it cannot have an explicit extends or implements clause. • An anonymous inner class must implement all the abstract methods in the superclass and the interface. • An anonymous inner class always uses the no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, the constructor is Object(). • Adapters implemented with anonymous inner classes are referred to as anonymous adapters. AnonymousInnerClassAdapterDemo

  34. JBuilder Optional Creating Listener Automatically in Forte You may demonstrate how JBuilder generates anonymous listener to process events.

  35. NetBeansOptional Creating Listener Automatically in NetBeans You may demonstrate how NetBeans generates anonymous listener to process events.

  36. Eclipse Optional Creating Listener Automatically in Eclipse You may demonstrate how Eclipse generates anonymous listener to process events.

  37. Model-View-Controller (MVC) The model-view-controller (MVC) approach is a way of developing components by separating data storage and handling from the visual representation of the data. The component for storing and handling data, known as a model, contains the actual contents of the component. The component for presenting the data, known as a view, handles all essential component behaviors. It is the view that comes to mind when you think of the component. It does all the displaying of the components. The controller is a component that is usually responsible for obtaining data.

  38. Benefits of MVC It makes multiple views possible so that data can be shared through the same model. For example, a model storing student names can simultaneously be displayed in a combo box or in a list box It simplifies the task of writing complex applications and makes the components scalable and easy to maintain. Changes can be made to the view without affecting the model, and vice versa

  39. Synchronization between Model and View A model contains data, whereas a view makes the data visible. Once a view is associated with a model, it immediately displays updates to the model. This ensures that all the views of the model display the same data consistently. To achieve consistency and synchronize the model with its dependent views, the model should notify the views when there is a change in a property in the model that is used in the view. In response to a change notification, the view is responsible for redisplaying the viewing area affected by the property change. The JDK event delegation model provides a superior architecture for supporting MVC component development. The model can be implemented as a source with appropriate event and event listener registration methods. The view can be implemented as a listener. Thus, if data are changed in the model, the view will be notified. To enable the selection of the model from the view, simply add the model as a property in the view with a set method.

  40. Example 21.6 Developing Model-View-Controller Components Problem: The example creates a model named CircleModel, a view named CircleView and a controller named CircleControl. CircleModel stores the properties (radius, filled, and color) that describe a circle. filled is a boolean value that indicates whether a circle is filled. CircleView draws a circle according to the properties of the circle. CircleControl enables the user to enter circle properties from a graphical user interface. Create an applet with two buttons named Show Controller and Show View. When click the Show Controller button, the controller is displayed in a frame. When click the Show View button, the view is displayed in a separate frame. view controller

  41. CircleModel The circle model stores the data and notifies any change of data to the listeners. The circle model contains properties radius, filled, and color, as well as the registration/deregistration methods for action event. CircleModel

  42. CircleView The view implements ActionListener to listen for notifications from the model. It contains the model as its property. When a model is set in the view, the view is registered with the model. The view extends JPanel to override the paintComponent method to draw the circle according to the properties values specified in the mode. CircleView

  43. CircleController The controller presents a GUI interface that enables the user to enter circle properties radius, filled, and color. It contains the model as its property. You can use the setModel method to associate a circle model with the controller. It uses a text field to obtain a new radius; a combo box to obtain a boolean value to specify whether the circle is filled; a JcolorChooser component to let the user select a color for the circle. CircleController

  44. Putting Things Together Finally, let us create an applet named MVCDemo with two buttons Show Controller and Show View. The Show Controller button displays a controller in a frame and the Show View button displays a view in a separate frame. MVCDemo Run

  45. MVC Variations One variation of the model-view-controller architecture is to combine the controller with the view. In this case, a view not only presents the data, but is also used as an interface to interact with the user and accept user input. Another variation of the model-view-controller architecture is to add part of the data from the model to the view so that the frequently used data can be accessed directly from the view.

  46. Swing MVC NOTE: Swing components are designed using the MVC architecture. Each Swing GUI component is a view that uses a model to store data. Many components contain part of the data in the model so that they can be accessed directly from the component. Swing MVC architecture will be further discussed in Chapter 25, “Advanced Swing Components.”

  47. Note to the Instructor You may cover Chapter 24 now.

More Related