jScrollPane.viewport如果添加了viewport.view MouseListener,则MouseListener不起作用

时间:2014-06-02 04:51:27

标签: java swing jscrollpane mouselistener

我有一个带有这种方法的JScrollPane子类。

private MouseListener currentViewportMouseListener;
private MouseMotionListener currentViewportMouseMotionListener;
private MouseListener currentScrollableMouseListener;
private MouseMotionListener currentScrollableMouseMotionListener;

@Override
public void setViewportMouseListener(MouseListener mouseListener)
{
    if (currentViewportMouseListener == mouseListener) return;

    viewport.removeMouseListener(currentViewportMouseListener);
    viewport.addMouseListener(mouseListener);
    currentViewportMouseListener = mouseListener;
}

@Override
public void setViewportMouseMotionListener(MouseMotionListener mouseMotionListener)
{
    if (currentViewportMouseMotionListener == mouseMotionListener) return;

    viewport.removeMouseMotionListener(currentViewportMouseMotionListener);
    viewport.addMouseMotionListener(mouseMotionListener);
    currentViewportMouseMotionListener = mouseMotionListener;
}

@Override
public void setScrollableMouseListener(MouseListener mouseListener)
{
    if (currentScrollableMouseListener == mouseListener) return;

    viewport.getView().removeMouseListener(currentScrollableMouseListener);
    viewport.getView().addMouseListener(mouseListener);
    currentScrollableMouseListener = mouseListener;
}

@Override
public void setScrollableMouseMotionListener(MouseMotionListener mouseMotionListener)
{
    if (currentScrollableMouseMotionListener == mouseMotionListener) return;

    viewport.getView().removeMouseMotionListener(currentScrollableMouseMotionListener);
    viewport.getView().addMouseMotionListener(mouseMotionListener);
    currentScrollableMouseMotionListener = mouseMotionListener;
}

所以当我使用第一对方法时,确定。

    scrollPane.setViewportMouseListener(mouseListener);
    scrollPane.setViewportMouseMotionListener(mouseListener);

但是,如果我同时设置视口和视图侦听器,则视口鼠标(Motion)侦听器不会捕获任何事件。

    scrollPane.setViewportMouseListener(mouseListener);
    scrollPane.setViewportMouseMotionListener(mouseListener);

    scrollPane.setScrollableMouseListener(scrollableMouseListener);
    scrollPane.setScrollableMouseMotionListener(scrollableMouseListener);

我在调试器中检查了它,视口和视图监听器属性填充正确(不同的对象)。

1 个答案:

答案 0 :(得分:0)

public void constructorOrMethodThatCreatesViewport()
{
    // ...
    this.viewport.addMouseListener(mainMouseListener);
    this.viewport.addMouseMotionListener(mainMouseListener);
}

private MouseListener currentViewportMouseListener;
private MouseMotionListener currentViewportMouseMotionListener;
private MouseListener currentScrollableMouseListener;
private MouseMotionListener currentScrollableMouseMotionListener;
private final MyMouseListener mainMouseListener = new MyMouseListener();

private class MyMouseListener implements MouseListener, MouseMotionListener
{
    private MouseEvent viewportToScrollableEvent(MouseEvent event)
    {
        Rectangle viewRect = viewport.getViewRect();
        MouseEvent r = new MouseEvent((Component) event.getSource(), 
                event.getID(), event.getWhen(), event.getModifiers(), 
                event.getX()+viewRect.x, event.getY()+viewRect.y, 
                Math.abs(event.getX()+viewRect.x), 
                Math.abs(event.getY()+viewRect.y), 
                event.getClickCount(), event.isPopupTrigger(), 
                event.getButton());
        return r;
    }

    public void mouseClicked(MouseEvent e)
    {
        if (null != currentScrollableMouseListener)
        {
            currentScrollableMouseListener.mouseClicked(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseListener)
        {
            currentViewportMouseListener.mouseClicked(e);
        }
    }

    public void mousePressed(MouseEvent e)
    {
        if (null != currentScrollableMouseListener)
        {
            currentScrollableMouseListener.mousePressed(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseListener)
        {
            currentViewportMouseListener.mousePressed(e);
        }
    }

    public void mouseReleased(MouseEvent e)
    {
        if (null != currentScrollableMouseListener)
        {
            currentScrollableMouseListener.mouseReleased(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseListener)
        {
            currentViewportMouseListener.mouseReleased(e);
        }
    }

    public void mouseEntered(MouseEvent e)
    {
        if (null != currentScrollableMouseListener)
        {
            currentScrollableMouseListener.mouseEntered(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseListener)
        {
            currentViewportMouseListener.mouseEntered(e);
        }
    }

    public void mouseExited(MouseEvent e)
    {
        if (null != currentScrollableMouseListener)
        {
            currentScrollableMouseListener.mouseExited(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseListener)
        {
            currentViewportMouseListener.mouseExited(e);
        }
    }

    public void mouseDragged(MouseEvent e)
    {
        if (null != currentScrollableMouseMotionListener)
        {
            currentScrollableMouseMotionListener.mouseDragged(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseMotionListener)
        {
            currentViewportMouseMotionListener.mouseDragged(e);
        }
    }

    public void mouseMoved(MouseEvent e)
    {
        if (null != currentScrollableMouseMotionListener)
        {
            currentScrollableMouseMotionListener.mouseMoved(viewportToScrollableEvent(e));
        }
        if (null != currentViewportMouseMotionListener)
        {
            currentViewportMouseMotionListener.mouseMoved(e);
        }
    }

}