Language Translator

Event Delegation Model

Q. Explain the Event Delegation Model.

Ans. The Event Delegation Model is Java’s way of handling user/process generated events. In this model, a source generates an event which is sent to one or more listeners registered with it. The listener then processes the event accordingly.



Q. What is an Event?

Ans. An event is an object that describes a state change in an event source.
For example, a button is an event source and pressing of the button is an event.

Q. What is an Event Source?

Ans. An event source is an object that generates an event. For example, a button is an event source.

Q. What is a Listener?

Ans. A listener is an object that is notified when an event occurs. There are two conditions to be fulfilled in order to become a valid listener;
1. The listener must be registered with at least one of the event sources.
2. It must implement methods that receive and process such events.



Q. The methods that receive and process events are defined in a set of interfaces in the _______ package.

Ans. java.awt.event

Q. Is it necessary to register a listener with an event source?

Ans. Yes, it is necessary to register a listener with an event source because an event notification is sent to only those listeners that are registered with the event source. All registered listeners receive a copy of the event object.

Q. How can a listener register itself with an event source?

Ans. The general form of the registration method is:

public void addTypeListener(new TypeListener())

Here, Type is the name of the event and new TypeListener() is an instance of the event listener object.

Some event sources may allow only one listener to be registered. The general form of such a registration method is:

public void addTypeListener(new TypeListener()) throws java.util.TooManyListenersException

Q. What is multicasting and unicasting?

Ans. When an event occurs, all the registered listeners are notified and sent a copy of the event object. This is known as multicasting.
If there is only one registered listener, then this process is called as unicasting.

Q. Name the event objects and their respective listener interfaces and Adapter Classes for the following events:
1. A button is pressed, a list item is double-clicked, or a menu item is selected.
2. A component is hidden, moved, resized or becomes visible.
3. A component is added to or removed from a container.
4. A check box is clicked.
5. A key is pressed.
6. Mouse is moved or clicked.
7. Value of a text area is changed.
8. A window is opened or closed.


Ans. Event Listener Interface Adapter Class

1. ActionEvent ActionListener None
2. ComponentEvent ComponentListener ComponentAdapter
3. ContainerEvent ContainerListener ContainerAdapter
4. ItemEvent ItemListener None
5. KeyEvent KeytListener KeyAdapter
6. MouseEvent MouseListener MouseAdapter
7. TextEvent TextListener None
8. WindowEvent WindowListener WindowAdapter

Q. Why are there no adapter classes for:
1. ActionEvent
2. ItemEvent
3. TextEvent

Ans. All these classes contain only one method. So, there is no need to write an adapter classes for them.

Q. What is an adapter class?

Ans. An adapter class provides an empty implementation of all the methods in an event listener interface. Such a class is useful when you want to receive and process only some of the events that are handled by a particular event listener interface. You can define a new class to act as an event listener by extending such an adapter class and implementing only those events in which you are interested.





















Q. What is an inner class? State an example.

Ans. An inner class is a class defined within another class or within an expression. For example,

public class MousePressedDemo extends Applet
{
public void init()
{
addMouseListener(new MyMouseAdapter(this));
}
}

class MyMouseAdapter extends MouseAdapter
{
MousePressedDemo mousePressedDemo;
//single argument constructor.
public MyMouseAdapter(MousePressedDemo mpd)
{
this.mousePressedDemo=mpd;
}
public void mousePressed(MouseEvent me)
{
mousePressedDemo.showStatus(“Mouse Clicked”);
}
}

Q. What is an Anonymous Inner Class? State an example.

Ans. An Anonymous Inner Class is one that is not assigned a name. For example,

public class AnonymousInnerClassDemo extends Applet
{
public void init()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
showStatus(“Mouse Clicked”);
}
});
}
}

No comments:

Post a Comment