Question

When adding a MouseListener to a JToolBar,

jToolbar.addMouseListener(new MouseInputAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
        log.debug(e.getPoint());
    }
});

The event only fires when clicked outside the JToolBar's gripper.

If I override BasicToolBarUI's createDockingListener():

@Override
protected MouseInputListener createDockingListener() {
    return new MouseInputAdapter() {
        @Override
        public void mousePressed(MouseEvent evt) {
            log.debug(e.getPoint());
        }
    }
}

The event will fire when clicked on the gripper.

So my question is, why? Is the MouseEvent consumed in the dockingListener? But I don't see any code that consumes the event.

Was it helpful?

Solution

The MouseEvent is being automatically consumed by the Container at a number of points for a number of different reasons (some relating to how the native peer needs to deal with the event)...

Most notably in the private method Container#processMouseEvent, but it could be consumed before then

This basically means, when you attach a MouseListener to any component, it will consume all mouse events going to any component (or part thereof) that it resides above.

Think of mouse events like rain. When a raindrop hits something, it stops.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top