如何在java swing中的jscrollbar拖动期间获取光标位置?

时间:2015-07-27 21:25:48

标签: java swing hover cursor-position jscrollbar

我有一个鼠标监听器连接到垂直的jScrollbar。内容的水平方面由悬停位置控制。现在,当用户拖动垂直滚动条手柄时,内容垂直滚动就好了,但它们只能看到最右边的内容(水平说话),因为光标位于jPanel的右侧。

我想要做的是,一旦用户点击拖动垂直滚动条,我想让他们将光标悬停在内容上(当鼠标按钮关闭时)并更新内容的水平位置

然而,即使我已经为我的jscrollbar的鼠标监听器添加了一个过度使用的mouseDragged函数来更新手动维护的悬停位置,它也不会被触发。这是我的代码:

getSecondaryScrollBar().addMouseListener(new MouseAdapter() {
    ...
    @Override
    public void mouseDragged(final MouseEvent e) {
        debug("Hover pixel position during drag: [" + e.getX() + "]",8);
        if(e.getX() < 0) {
            //Set the hover position to wherever the cursor has wandered to while dragging the secondary scrollbar
            map.setHoverIndex(map.getIndex(scrollPane.getViewport().getSize().width + e.getX()));
        } else {
            //Set the hover position as the far right label
            map.setHoverIndex(-1);
        }
        repaint();
    }
});

拖动滚动条时不会打印调试消息。标签垂直滚动。我甚至通过添加断点来调试,所以我知道mouseDragged甚至没有被调用。我也尝试了mouseMoved函数,结果相同。

我有一个调整侦听器,它接受一个处理内容垂直拖动的调整事件对象,但它无法访问鼠标事件getX()函数。

当光标位于jPanel上时,我还将mouseDragged和mouseMoved函数作为主类的一部分。它们用于控制水平位置和进行选择。两者都很出色:

@Override
public void mouseMoved(final MouseEvent e) {
    setHoverPosition(e);
    hoverIndex = map.getIndex(getPrimaryHoverPosition(e));
    repaint();
}
@Override
public void mouseDragged(final MouseEvent e) {
    setHoverPosition(e);
    hoverIndex = map.getIndex(getPrimaryHoverPosition(e));
    map.setHoverIndex(hoverIndex);
    debug("Selecting from " + map.getSelectingStart() + " to " + map.getIndex(getPrimaryHoverPosition(e)),8);
    repaint();
}

最后,这是一个屏幕录音,应该澄清我在做什么,以防我的描述不足:

https://dl.dropboxusercontent.com/u/87939936/StackOverflow/getX_on_scrollbar_drag_question.mov

有人知道如何在拖动垂直滚动条的过程中获取光标位置吗?

2 个答案:

答案 0 :(得分:1)

mouseDragged and mouseMoved (attached to the jscrollbar) are not called during a jscrollbar drag and the AdjustmentEvent object has no way to obtain the cursor position or to get access to an object that can return it either. Therefor you must find the cursor position externally using something like MouseInfo.getPointerInfo().getLocation() (see Get Mouse Position).

Then, to convert that coordinate to be relative to the jPanel, you can use SwingUtilities.convertPointFromScreen().

Here, I have updated the cursor position in an overridden updateBuffer function which then calls its parent to draw everything. Note that, I only update the hover position when the scrollbar is being dragged (e.g. areLabelsBeingScrolled()) which simply returns a boolean that is set to true when the mouse clicks on a scrollbar and set to false when it is released.

@Override
public void updateBuffer(final Graphics g,final Dimension offscreenSize) {
    if(areLabelsBeingScrolled()) {
        Point p = MouseInfo.getPointerInfo().getLocation();
        SwingUtilities.convertPointFromScreen(p,getComponent());
        debug("Cursor x coordinate relative to column labels: [" + p.x + "]",8);
        int hDI = map.getIndex(p.x); //Hover Data Index
        if(hDI > map.getMaxIndex()) {
            hDI = map.getMaxIndex();
        } else if (hDI < 0) {
            hDI = 0;
        }
        map.setHoverIndex(hDI);
    }
    super.updateBuffer(g,offscreenSize);
}

As you can see from this screen recording, it works quite well:

https://dl.dropboxusercontent.com/u/87939936/StackOverflow/getX_on_scrollbar_drag_answer.mov

Furthermore, to make cursor hovering changes smooth, I use a timer that causes updateBuffer to update continuously during the scrolling period. But that is ancillary to the question, so I won't go into further detail about it.

答案 1 :(得分:0)

private void labelMousePressed(java.awt.event.MouseEvent evt) {                                    
    xMouse = evt.getX();
    yMouse = evt.getY();
} 

通过此代码,您可以在标签上按下光标时获取光标的位置。

我希望这会对你有所帮助。