views:

41

answers:

1

Hi, 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).

+2  A: 

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 
Tomas Narros