AWTEventMulticast

A

Adam

.... what is it for?

After reading the javadocs I'm a bit confused.
It says:

public myComponent extends Component {
ActionListener actionListener = null;

public synchronized void addActionListener(ActionListener l) {
actionListener = AWTEventMulticaster.add(actionListener, l);
}
public synchronized void removeActionListener(ActionListener l) {
actionListener = AWTEventMulticaster.remove(actionListener, l);
}
public void processEvent(AWTEvent e) {
// when event occurs which causes "action" semantic
if (actionListener != null) {
actionListener.actionPerformed(new ActionEvent());
}
}

So calling actionListener.actionPerformed(...) in processEvent
will notify all ActionListeners added through addActionListener?

I always thought that addXXXListener puts its parameter in some array
or vector, and during processXXXEvent this array
is iterated and actionPerformed is called at all its elements.

Can someone explain how does Multicaster implement that?

Another thing I'm wondering about is enableEvents(...) method.
processXXXEvent shall not be called unless enableEvents with
appropriate flag was called or some corresponding
XXXListener was registered.
How is this implemented in AWT? I mean it probably is some simple
'if' statement, but where is that code? Where do processXXXEvent
methods get called from?

Adam
 
R

Ryan Stewart

Adam said:
... what is it for?

After reading the javadocs I'm a bit confused.
It says:

public myComponent extends Component {
ActionListener actionListener = null;

public synchronized void addActionListener(ActionListener l) {
actionListener = AWTEventMulticaster.add(actionListener, l);
}
public synchronized void removeActionListener(ActionListener l) {
actionListener = AWTEventMulticaster.remove(actionListener, l);
}
public void processEvent(AWTEvent e) {
// when event occurs which causes "action" semantic
if (actionListener != null) {
actionListener.actionPerformed(new ActionEvent());
}
}

So calling actionListener.actionPerformed(...) in processEvent
will notify all ActionListeners added through addActionListener?

I always thought that addXXXListener puts its parameter in some array
or vector, and during processXXXEvent this array
is iterated and actionPerformed is called at all its elements.

Can someone explain how does Multicaster implement that?

If you're really interested, look into the source code of
AWTEventMulticaster. I partially deciphered it a while back. IIRC, a
multicaster holds a maximum of two objects of type EventListener. The catch
is that a multicaster is an EventListener. It creates a chain of
listeners/multicasters. Nothing is stored in an array or collection, though
a method in AWTEventMulticaster will return all its listeners as an array.
Most of the code is fairly straightforward.
Another thing I'm wondering about is enableEvents(...) method.
processXXXEvent shall not be called unless enableEvents with
appropriate flag was called or some corresponding
XXXListener was registered.
How is this implemented in AWT? I mean it probably is some simple
'if' statement, but where is that code? Where do processXXXEvent
methods get called from?

Adam

Code in java.awt.Component:
if (eventEnabled(e)) {
processEvent(e);
}

Pretty simple, huh? Well, not so much if you dig into it a bit, but again,
it's pretty straightforward. Read the source code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top