Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing events to parent

I'd like to create an app where some events are supposed to be handled as if they were delivered to parent containers. For example I've got a JPanel which contains JLabel. The top JPanel implements mousepress and dragging right now. What do I need to do, in order to make the events look like they arrived to JPanel instead of the label itself. (changing source object is important)

Is there some better solution than actually implementing the events and replicating them in the parent? (this would get tedious after some objects with >5 children).

like image 615
viraptor Avatar asked Sep 29 '10 01:09

viraptor


People also ask

How to pass event from parent to child in React?

To pass an onChange event handler to a child component in React: Define the event handler function in the parent component. Pass it as a prop to the child component, e.g. <Child handleChange={handleChange} /> . Set it to the onChange prop on the input field in the child.


2 Answers

At your event listener, you can dispatch the event to the parent component.

Being myEvent the event handling function argument:

Component source=(Component)myEvent.getSource();
source.getParent().dispatchEvent(myEvent);

But this solution implies creating a new EventListener for each element to add.

So, you could create a single event handler and reuse it, adding it to all the chosen children, like this:

final Container parent=this; //we are a the parent container creation code
MouseListener myCommonListener=new MouseListener() {
    @Override
    public void mouseClicked(MouseEvent e) {
        parent.dispatchEvent(e);
    }
    @Override
    public void mouseEntered(MouseEvent e) {
        parent.dispatchEvent(e);
    }
    @Override
    public void mouseExited(MouseEvent e) {
        parent.dispatchEvent(e);
    }
    @Override
    public void mousePressed(MouseEvent e) {
        parent.dispatchEvent(e);
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        parent.dispatchEvent(e);
    }
};

JLabel label=new JLabel("This is the first Label");
label.addMouseListener(myCommonListener);

JLabel label2=new JLabel("This is the second Label");
label2.addMouseListener(myCommonListener);
//... and so on 
like image 94
Tomas Narros Avatar answered Sep 20 '22 18:09

Tomas Narros


You should convert event before dispatching it to the parent. Conversion includes coordinates translation to parent-relative.

public class RedispatchingMouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener{

    public void mouseClicked(MouseEvent e) {
        redispatchToParent(e);
    }

    public void mousePressed(MouseEvent e) {
        redispatchToParent(e);
    }

    public void mouseReleased(MouseEvent e) {
        redispatchToParent(e);
    }

    public void mouseEntered(MouseEvent e) {
        redispatchToParent(e);
    }

    public void mouseExited(MouseEvent e) {
        redispatchToParent(e);
    }

    public void mouseWheelMoved(MouseWheelEvent e){
        redispatchToParent(e);
    }

    public void mouseDragged(MouseEvent e){
        redispatchToParent(e);
    }

    public void mouseMoved(MouseEvent e) {
        redispatchToParent(e);
    }

    private void redispatchToParent(MouseEvent e){
        Component source = (Component) e.getSource();
        MouseEvent parentEvent = SwingUtilities.convertMouseEvent(source, e, source.getParent());
        source.getParent().dispatchEvent(parentEvent);
    }
}
like image 44
Wojciech Wirzbicki Avatar answered Sep 21 '22 18:09

Wojciech Wirzbicki