[Swing] how to relay events to other components

T

T E Schmitz

Hello,

I would like to achieve the following:

Scenario:
Component A notifies component B about an event e1. B knows that there
are are various other components interested to know about this event. Is
it possible for B to relay this event to components C,D,+E? Or do C,D+E
have to be registered as listeners with A in the first place?
Also, another type of event e2 will cause the same action in C,D+E.
A doesn't [need to] know about C,D+E.
C,D+E don't care whether e1 or e2 trigger of the action.

Example:
Tabbed pane: When a tab comes to the front the data need to be refreshed
from the datastore. The panel associated with the tab contains several
"sub-panels", which are implemented as separate classes. They don't care
what the source of the event was. All they need is a trigger to refresh
themselves.

Only solution I can think of:
B provides A with a list of listeners collected from C,D+E.

Maybe I am just overlooking the blindingly obvious.
 
C

Chris Smith

T said:
Scenario:
Component A notifies component B about an event e1. B knows that there
are are various other components interested to know about this event. Is
it possible for B to relay this event to components C,D,+E? Or do C,D+E
have to be registered as listeners with A in the first place?
Also, another type of event e2 will cause the same action in C,D+E.
A doesn't [need to] know about C,D+E.
C,D+E don't care whether e1 or e2 trigger of the action.

The ideal solution would have B keeping a ListenerList of interested
objects for a semantic event that you define, and when B receives a
lower-level event, it responds by firing the semantic event to its
listeners.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Karl von Laudermann

T E Schmitz said:
Hello,

I would like to achieve the following:

Scenario:
Component A notifies component B about an event e1. B knows that there
are are various other components interested to know about this event. Is
it possible for B to relay this event to components C,D,+E? Or do C,D+E
have to be registered as listeners with A in the first place?
Also, another type of event e2 will cause the same action in C,D+E.
A doesn't [need to] know about C,D+E.
C,D+E don't care whether e1 or e2 trigger of the action.

Are you sure that components C, D, and E even need to know about the
actual events being sent? Or can B simply do stuff with C, D and E as
part of its handling of the event? For instance, let's look at your
example:
Example:
Tabbed pane: When a tab comes to the front the data need to be refreshed
from the datastore. The panel associated with the tab contains several
"sub-panels", which are implemented as separate classes. They don't care
what the source of the event was. All they need is a trigger to refresh
themselves.

It seems to me that the panel in the tab is the only event listener,
and it handles the event by calling refresh() on the sub-panels. Not
only do the sub-panels not care what the source of the event was,
they don't even care that an event was sent; they just know that
they're being told to refresh.
Only solution I can think of:
B provides A with a list of listeners collected from C,D+E.

If C, D, and E do for some reason really need to be event listeners
themselves, then this is one way of doing it. Another way is for you
to create a custom EventListener subinterface, and a correstponding
custom Event subclass, and have B fire its own custom events, which C,
D, and E listen for. But this might be overkill.
Maybe I am just overlooking the blindingly obvious.

If B actually knows about/owns/controls C, D, and E, then as I said
above, the latter don't need to actually handle the events; B should
just tell them what to do, as part of its own event handling.
 
F

FISH

T E Schmitz said:
Hello,

I would like to achieve the following:

Scenario:
Component A notifies component B about an event e1. B knows that there
are are various other components interested to know about this event. Is
it possible for B to relay this event to components C,D,+E? Or do C,D+E
have to be registered as listeners with A in the first place?
Also, another type of event e2 will cause the same action in C,D+E.
A doesn't [need to] know about C,D+E.
C,D+E don't care whether e1 or e2 trigger of the action.

[snipped...]


As I understand it, you have several components which all need to take
action (an update/refresh) when a specific event occurs. You *could*
break the update code up across all the components, having each
component register itself as a listener with the event source, and
handling their own part of the task internally. Alternatively you
could put all the update code in one place (one event handler) external
to the components being manipulated, and register only one listener
with the event source.

Which you do depends upon the nature of your code. If this update/
refresh functionality is generally useful for said components, and is
likely to be re-used elsewhere, then place the code inside the actual
component itself and have each component take charge of its own updates.
If this functionality is a one-off, then it is far neater to group it
all together in one place, external to the components it is acting upon.

(Of course, your choice will also depend upon the 'visibility' of the
methods you need to call to manipulate the components when an event
happens. If a crutial method *must* have a very tight access modifier,
then you may not be able to manipulate it externally as you require -
although this could be a sign that there are short-comings in the way
that you designed the component class to begin with!)


-FISH- ><>
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top