Java Chap3
Java Chap3
The GUI programming is inherently event-driven; whenever a user initiates an activity such as a
mouse activity, clicks, scrolling, etc., each is known as an event that is mapped to a code to
respond to functionality to the user. This is known as event handling.
In this section, we will discuss event processing and how to implement the delegation event
model in Java. We will also discuss the different components of an Event Model.
But, the modern approach for event processing is based on the Delegation Model. It defines a
standard and compatible mechanism to generate and process events. In this model, a source
generates an event and forwards it to one or more listeners. The listener waits until it receives
an event. Once it receives the event, it is processed by the listener and returns it. The UI
elements are able to delegate the processing of an event to a separate function.
The key advantage of the Delegation Event Model is that the application logic is completely
separated from the interface logic.
In this model, the listener must be connected with a source to receive the event notifications.
Thus, the events will only be received by the listeners who wish to receive them. So, this
approach is more convenient than the inheritance-based event model (in Java 1.0).
In the older model, an event was propagated up the containment until a component was
handled. This needed components to receive events that were not processed, and it took lots of
time. The Delegation Event model overcame this issue.
Events
Events Sources
Events Listeners
Events
The Events are the objects that define state change in a source. An event can be generated as a
reaction of a user while interacting with GUI elements. Some of the event generation activities
are moving the mouse pointer, clicking on a button, pressing the keyboard key, selecting an item
from the list, and so on. We can also consider many other user operations as events.
The Events may also occur that may be not related to user interaction, such as a timer expires,
counter exceeded, system failures, or a task is completed, etc. We can define events for any of the
applied actions.
Event Sources
A source is an object that causes and generates an event. It generates an event when the internal
state of the object is changed. The sources are allowed to generate several different types of
events.
A source must register a listener to receive notifications for a specific event. Each event contains
its registration method. Below is an example:
A source should contain a method that unregisters a specific type of event from the listener if not
needed. Below is an example of the method that will remove the event from the listener.
The source provides the methods to add or remove listeners that generate the events. For
example, the Component class contains the methods to operate on the different types of events,
such as adding or removing them from the listener.
Event Listeners
An event listener is an object that is invoked when an event triggers. The listeners require two
things; first, it must be registered with a source; however, it can be registered with several
resources to receive notification about the events. Second, it must implement the methods to
receive and process the received notifications.
The methods that deal with the events are defined in a set of interfaces. These interfaces can be
found in the java.awt.event package.
For example, the MouseMotionListener interface provides two methods when the mouse is
dragged and moved. Any object can receive and process these events if it implements the
MouseMotionListener interface.
a.
ActionEvent Class:
1. An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a menu
item is selected.
2. The ActionEvent class defines four integer constants that can be used to identify any modifiers
associated with an action event: ALT_MASK, CTRL_MASK, META_MASK, and SHIFT_MASK.
In addition, there is an integer constant, ACTION_ PERFORMED, which can be used to identify
action
events.
3.ActionEvent has these three constructors:
ActionEvent(Object src, int type, String cmd)
ActionEvent(Object src, int type, String cmd, int modifiers)
ActionEvent(Object src, int type, String cmd, long when, int modifiers)
Here, src is a reference to the object that generated this event. The type of the event is specified
by
type, and its command string is cmd.
The argument modifiers indicates which modifier keys (ALT, CTRL, META, and/or SHIFT) were
pressed when the event was generated. The when parameter specifies when the event occurred.
String getActionCommand( )
You can obtain the command name for the invoking ActionEvent object by using the
getActionCommand( ) method, shown here:
The getModifiers( ) method returns a value that indicates which modifier keys (ALT, CTRL,
META,
and/or SHIFT) were pressed when the event was generated
long getWhen( )
The method get
java adapter classes provide the default implementation of listener interfaces. If you inherit the
adapter class, you will not be forced to provide the implementation of all the methods of listener
interfaces. So it saves code.
6.Define Packages.
a.Write a program to demonstrate User defined packages.
7.Explain the following :1) ActionEvent Class 2)MouseEvent class
8.Explain the architecture of J2EE with neat Diagram.
9.Define J2EE.
a.Describe the Multilayer Application of J2EE with neat diagram
a.
10.Write a note on types of JDBC Drivers
12.Write a program to display records from the table using JDBC
did not get
a.
16.Explain the concept of Input and Output Stream with a neat diagram.
a.Write a note on Output Stream
a.did not get
a.
18.Explain following InputStream Class methods 1) read() 2) available() 3)close() 4) mark()
5)reset 6) skip()