Component mouse events / children


#1

I have a listbox, and each row holds a number of components that allow the displayed contents to be edited. There is one area of blank space on the row component, and at the moment that is the only bit that responds to mouseDown(e) to allow for selection.

I understand this, and would like to know if there is a defined way of letting a component take mouse events if the children are clicked on. I’ve just discovered the ‘setInterceptsMouseClicks’ but i’m not sure if that will help, because the children need to respond in their own way too.

for instance, a label can be double-clicked for editing, but i’d like to be able to have a single click trigger the row’s “selection” function, i guess by calling the parent’s mouseDown. My thought is to just subclass a Label and override mouseDown to call the parent’s mouseDown.

does that sound like too much? seems like the most flexible option at the moment


#2

how about childComponent->addMouseListener( this, true );

from the docs…

void Component::addMouseListener  (  MouseListener *const  newListener,  
  const bool  wantsEventsForAllNestedChildComponents 
 )  

Registers a listener to be told when mouse events occur in this component. 

If you need to get informed about mouse events in a component but can't or don't want to override its methods, you can attach any number of listeners to the component, and these will get told about the events in addition to the component's own callbacks being called.

Note that a MouseListener can also be attached to more than one component.


Parameters:
newListener  the listener to register  
wantsEventsForAllNestedChildComponents  if true, the listener will receive callbacks for events that happen to any child component within this component, including deeply-nested child components. If false, it will only be told about events that this component handles.  

sure I used this once to get a parent to do its mouseDown when a child got it’s mouseDown.

may not be what your after. brains tired.


#3

Yes, the addMouseListener call is for doing exactly this kind of thing.

In this case though, I’d probably be inclined to use a row component that handles the clicks and calls ListBox::selectRowsBasedOnModifierKeys() to select itself. That’s better because you could, e.g. add a temporary TextEditor to the row without all the clicks on the TextEditor also being forwarded to the parent.


#4

brilliant stuff, thanks a lot for that guys. not sure how i managed to miss that!


#5