47
1 Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pears on Education, Inc. All rights reserved. 0-13-148952-6 Chapter 21 JavaBeans, Bean Events, and MVC Architecture Prerequisites for Part VII Chapter 21 JavaBeans, Bean Events, and MVC Chapter14 A pplets,Im ages, and A udio Chapter 22 Containers, Layout Managers, and Borders Chapter 23 Menus, Toolbars, Dialogs, and Internal Frames Chapter 24 Advanced Swing Components

Chapter 21 JavaBeans, Bean Events, and MVC Architecture

  • Upload
    anahid

  • View
    40

  • Download
    2

Embed Size (px)

DESCRIPTION

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). - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

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

Chapter 21 JavaBeans, Bean Events, and MVC Architecture

Prerequisites for Part VII

Chapter 21 JavaBeans, Bean Events, and MVC

Chapter 14 Applets, Images, and Audio

Chapter 22 Containers, Layout Managers, and Borders

Chapter 23 Menus, Toolbars, Dialogs, and Internal Frames

Chapter 24 Advanced Swing Components

Page 2: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

2Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

Page 3: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

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

Data members Methods Constructors

public class public no-arg constructor serializable may have accessor/mutator methods may have registration/deregistration methods

class

JavaBeans Component Minimum requirement

Optional requirement

Page 4: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

4Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Page 5: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

5Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

JavaBeans and Builder ToolsNOTE: 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.

Page 6: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

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

Page 7: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

7Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Properties and Data FieldsProperties 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.

Page 8: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

8Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Bean EventsA 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.

Page 9: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

9Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

The Event Delegation Model

source: SourceClass

+addXListener(XListener listener)

listener: ListenerClass User Action

Trigger an event

XListener +handler(XEvent event)

Internal function of the source object

event: XEvent listener1 listener2 … listenern

+handler(XEvent

Register by invoking source.addXListener(listener);

Keep it a list

Invoke listener1.handler(event) listener2.handler(event) … listenern.handler(event)

Figure 12.2

Page 10: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

10Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Predefined Event Pairs(Event Classes and Listener Interface)

Examples:

ActionEvent/ActionListener

AdjustmentEvent/AdjustmentListener

AWTEvent EventObject

AdjustmentEvent

ComponentEvent

TextEvent

ItemEvent

ActionEvent

InputEvent

WindowEvent

MouseEvent

KeyEvent

ContainerEvent

FocusEvent

PaintEvent

ChangeEvent HyperLinkEvent InternalFrameEvent ListSelectionEvent

TableModelEvent

TableColumnEvent TreeModelEvent TreeSelectionEvent

TreeExpansionEvent javax.swing.event package

Page 11: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

11Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Examples of Event Pairs

java.awt.event.ActionEvent

actionCommand: String

modifier: int

when: long

+ActionEvent(source: Object, id: int, command: String)

+getActionCommand(): String

+getModifier(): int

+getWhen(): long

java.util.EventListener

java.util.EventObject

+EventObject(source: Object)

+getSource(): Object

java.awt.AWTEvent

java.awt.event.ActionListener

+actionListener(ActionEvent e): void

Page 12: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

12Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Source Components

Fire and process event by invoking the event handler from each listener in the vector

Source Component

Register listener method Deregister listener method

A vector (stores the listener objects)

Detect events

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

Page 13: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

13Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Listener Components

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

Process event

actionPerformed(ActionEvent e)

Source Component Listener Component

addActionListener(ActionListener l) removeActionListener(ActionListener l)

JButton jbt = new JButton(); // Create a source object MyListener listener = new MyListener (); // Create a listener object jbt.addActionListener(listener); // Register listener to the source

Test Class

Listener vector MyListener class implements ActionListener

JButton

Invoke listener’s actionPerformed method Generate an

event

Page 14: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

14Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Creating Custom Event PairsYou 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>.

Page 15: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

15Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

-tickCount: long

-tickInterval: long

+TickEvent(source: Object)

+getTickCount(): long

+getTickInterval(): long

+setTickCount(tickCount: long): void

+setTickInterval(tickInterval: long): void

java.util.EventListener

java.util.EventObject

TickListener

+handleTick(TickEvent e): void

TickEventTickEvent

TickListenerTickListener

Page 16: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

16Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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;

Page 17: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

17Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Page 18: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

18Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Page 19: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

19Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Example 21.2 Creating a Source Component

TickTick

Tick

-tickCount: long

-tickInterval: long

-maxInterval: long

-minInterval: long

-step: long

-e: TickEvent

-tickListenerList: ArrayList

-suspended: boolean

-thread: Thread

+Tick()

+Tick(tickInterval: int, maxInterval: int, minInterval: int, step: int)

+resume(): void

+suspend(): void

+addTickListener(l: TickListener): void

+removeTickListener(l: TickListener): void

-processEvent(e: TickEvent): void

java.io.Serializable

java.lang.Runnable

JavaBeans property for tickCount (default 0).

JavaBeans property for tickInterval (default 100).

JavaBeans property for maxInterval (default 5000).

JavaBeans property for minInterval (default 1).

JavaBeans property for step (default 0).

Tick event created from the Tick object.

Stores the TickEvent listeners.

Indicates whether tick is suspended.

The thread to run tick.

Creates a Tick object with default properties.

Creates a Tick object with the specified properties.

Resumes the tick.

Suspends the tick.

Adds a new listener to this object.

Removes a listener from this object.

Processes the event.

Page 20: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

20Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

DisplayMovingMessageDisplayMovingMessage RunRun

JApplet

DisplayMovingMessage -tick: Tick -messagePanel: MovingMessage +DisplayingMessage()

Tick

TickEvent

MovingMessage

+handleTick(TickEvent e)

TickListener

Page 21: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

21Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Process event

handleTick(TickEvent e)

Source Component Listener Component

addTickListener(TickListener l) removeTickListener(TickListener l)

Tick tick = new Tick(); // Create a source object MovingMessage messagePanel = new MovingMessage(); // Create a listener object tick.addTickListener(messagePanel); // Register listener to the source

DisplayMovingMessage

Listener vector MovingMessage class implements TickListener

Tick

Invoke listener’s handleTick method Generate an

event

Page 22: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

22Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Page 23: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

23Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

TickUsingActionEventTickUsingActionEvent

RunRun

JApplet

DisplayingMessageUsingActionEvent -tickUsingActionEvent1: TickUsingActionEvent -messagePanel: MovingMessageNew +DisplayingMessageUsingActionEvent()

TickUsingActionEvent

ActionEvent

MovingMessageNew

+actionPerformed(ActionEvent e)

ActionListener

DisplayingMessageUsingActionEventDisplayingMessageUsingActionEvent

Page 24: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

24Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Process event

actionPerformed(ActionEvent e)

Source Component Listener Component

addActionListener(ActionListener l) removeActionListener(ActionListener l)

TickUsingActionEvent tick = new TickUsingActionEvent (); // Create a source object MovingMessageNew messagePanel = new MovingMessageNew(); // Create a listener object tick.addActionListener(messagePanel); // Register listener to the source

DisplayingMessageUsingActionEvent

Listener vector MovingMessageNew class implements ActionListener

TickUsingActionEvent

Invoke listener’s actionPerformed method Generate an

event

Page 25: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

25Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Source Object

Trigger an event

Adaptee for processing the event

Register a listener object

EventObject

Implements Event Handler

Notify adapter by invoking the adapter’s

handler

Generate an event

User or System action

Standard Listener Adapter

Adapter

Delegates if necessary

Listener Interface

Adapter is registered as a handler

e.g., MouseAdapter

e.g., MouseListener

Page 26: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

26Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Source Object

Trigger an event

Adaptee for processing the event

Register a listener object

EventObject

Implements Event Handler

Notify adapter by invoking the adapter’s

handler

Generate an event

User or System action

Standard Listener Adapter

Adapter

Delegates if necessary

Listener Interface

Adapter is registered as a handler

e.g., MouseAdapter

e.g., MouseListener

Page 27: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

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

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.

Page 28: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

28Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Convenience Listener AdapterAn 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.

Page 29: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

29Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Standard AdaptersA 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

StandardAdapterDemoStandardAdapterDemo RunRun

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.

Page 30: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

30Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Source Component, Adapter, and Adaptee Interactions

TimerEventSource1

Event processEvent(ActionEvent e) actionPerformed(ActionEvent e)

Source Component Adapter

TickUsingActionEvent

Adaptee

actionPerformed(ActionEvent e)

ControlMovingMessage MyActionListenerAdapter

TimerEventSource1

Mouse pressed

processEvent(MouseEvent e) mousePressed(MouseEvent e)

Source Component Adapter

ControlMovingMessage

Adaptee

mousePressed(ActionEvent e)

ControlMovingMessage MyMouseListenerAdapter

Mouse released

processEvent(MouseEvent e) mouseReleased(MouseEvent e) mouseReleased(ActionEvent e)

Page 31: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

31Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

InnerClassAdapterDemoInnerClassAdapterDemo RunRun

Page 32: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

32Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

}

Page 33: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

33Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Anonymous Inner Class AdaptersSince 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.

AnonymousInnerClassAdapterDemoAnonymousInnerClassAdapterDemo

Page 34: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

34Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Creating Listener Automatically in Forte

You may demonstrate how JBuilder generates anonymous listener to process events.

JBuilder Optional

Page 35: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

35Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Creating Listener Automatically in NetBeans

You may demonstrate how NetBeans generates anonymous listener to process events.

NetBeansOptional

Page 36: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

36Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Creating Listener Automatically in Eclipse

You may demonstrate how Eclipse generates anonymous listener to process events.

EclipseOptional

Page 37: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

37Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Model View

Obtain input

Controller

Store data Display data

Page 38: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

38Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Benefits of MVCIt 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

Page 39: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

39Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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.

Page 40: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

40Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

Page 41: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

41Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

CircleModelThe 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

-radius: double

-filled: boolean

-color: java.awt.Color

+addActionListener(l: ActionListener): void

+removeActionListener(l: ActionListener): void

-processEvent(e: ActionEvent): void

_

The radius of this circle.

True if the circle is filled.

The color of the circle.

Adds a new listener to this object.

Removes a listener from this object.

Processes the event.

CircleModelCircleModel

Page 42: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

42Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

CircleViewCircleView

CircleView

-model: CircleModel

+actionPerformed(e: ActionEvent): void

+paintComponent(g: Graphics): void

_

Stores the circle model.

Implements this method to update the view.

Paints the view.

JPanel

ActionListener

Page 43: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

43Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

CircleControllerCircleController

Page 44: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

44Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Putting Things TogetherFinally, 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.

MVCDemoMVCDemo RunRun

Page 45: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

45Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

MVC VariationsOne 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.

Model View (Controller)

Model may be modified via view

Controller is part of the view

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.

Page 46: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

46Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

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

Page 47: Chapter 21  JavaBeans, Bean Events, and MVC Architecture

47Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6

Note to the InstructorYou may cover Chapter 24 now.