One shared MouseListener or many distributed

M

Martijn Mulder

I have 20 components from a common base that respond to mouse input. Only one
component is active at a time and is accessed through a central component that
is assigned one of the 20 instances.

What is the best way to organize the mouse input? I can

1. add a MouseListener to every component at startup, or

2. add a MouseListener to the central component every time another component
gets active.

Is there a penalty on either method in terms of performance, memory consumption
or otherwise?
 
G

Gerbrand van Dieijen

Martijn Mulder schreef:
I have 20 components from a common base that respond to mouse input. Only one
component is active at a time and is accessed through a central component that
is assigned one of the 20 instances.

What is the best way to organize the mouse input? I can

1. add a MouseListener to every component at startup, or

2. add a MouseListener to the central component every time another component
gets active.

Does the same thing have to happen when the event happens? You could
then just add the mouselistener to every components add start up.

I think it's best to do this during initializations. This results in a
smaller chance of bugs, because possible errors come up earlier. Your
code is more readable.
Is there a penalty on either method in terms of performance, memory consumption
or otherwise?

Important rule in computer science, never optimize beforehand! There's
nothing more inefficient, than bugs in software.
 
M

Martijn Mulder

I have 20 components from a common base that respond to
Does the same thing have to happen when the event
happens? You could then just add the mouselistener to
every components add start up.

I think it's best to do this during initializations. This
results in a smaller chance of bugs, because possible
errors come up earlier. Your code is more readable.




Important rule in computer science, never optimize
beforehand! There's nothing more inefficient, than bugs
in software.



Thank you for your advice. I implemented 20 mouseListeneres for 20 Components,
but all 19 invisible Components reacted on the operations performed on the one
visible Component. How was I to know? So now I just have one Component that gets
assigned one of the 20 possible Components. This central Component is the only
one that acts as a mouseListener.

I noticed a remarkable slow down in my program when I had many many
mouseListeners active. It might be caused by something else, but probably this
whole event processing is time-consuming.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top