简体   繁体   English

在Java swing中的jscrollbar拖动过程中,如何获取光标位置?

[英]How do you get the cursor position during a jscrollbar drag in java swing?

I have a mouse listener attached to a vertical jScrollbar. 我在垂直jScrollbar上附加了一个鼠标侦听器。 The horizontal aspect of the content is controlled by hover position. 内容的水平方向由悬停位置控制。 Right now, when the user drags the vertical scrollbar handle, the content scrolls vertically just fine, but they can only see the right-most content (horizontally speaking) because the cursor is off the right side of the jPanel. 现在,当用户拖动垂直滚动条手柄时,内容可以很好地垂直滚动,但由于光标位于jPanel的右侧,因此他们只能看到最右边的内容(水平地说)。

What I would like to do is, once the user clicks to drag the vertical scrollbar, I want to allow them to hover the cursor over the content (while the mouse button is down) and have the horizontal position of the content updated. 我想做的是,一旦用户单击以拖动垂直滚动条,我希望允许他们将光标悬停在内容上(同时按下鼠标按钮),并更新内容的水平位置。

However, even though I have added an over-ridden mouseDragged function to my jscrollbar's mouse listener to update a manually maintained hover position, it is not getting fired. 但是,即使我在我的jscrollbar的鼠标侦听器中添加了重写的mouseDragged函数来更新手动维护的悬停位置,也不会被触发。 Here's my code: 这是我的代码:

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();
    }
});

The debug message does not print upon drag of the scrollbar. 拖动滚动条时不会打印调试消息。 The labels are scrolling vertically. 标签垂直滚动。 I even debugged by adding a breakpoint, so I know mouseDragged is not even getting called. 我什至通过添加一个断点进行调试,所以我知道mouseDragged甚至没有被调用。 I also tried the mouseMoved function with the same result. 我也尝试了mouseMoved函数,结果相同。

I have an adjustment listener that takes an adjustment event object that handles the vertical dragging of content, but it does not have access to the mouse event getX() function. 我有一个调整侦听器,该侦听器带有一个调整事件对象,该对象处理内容的垂直拖动,但是它无法访问鼠标事件getX()函数。

I also have mouseDragged and mouseMoved functions as a part of the main class for when the cursor is over the jPanel. 当光标位于jPanel上方时,我还将mouseDragged和mouseMoved函数作为主类的一部分。 They're used for controlling horizontal position and making selections. 它们用于控制水平位置和进行选择。 Both work splendidly: 两者都出色地工作:

@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();
}

Finally, here's a screen recording that should clarify what I'm doing in case my description is insufficient: 最后,这是一个屏幕录像,应该可以澄清我在做的描述不足的情况:

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

Does anyone know how to get the cursor position during a drag of the vertical scrollbar? 有谁知道如何在拖动垂直滚动条时获取光标位置?

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. 在jscrollbar拖动期间,不会调用mouseDragged和mouseMoved(附加到jscrollbar),并且AdjustmentEvent对象无法获取光标位置或访问可以返回它的对象。 Therefor you must find the cursor position externally using something like MouseInfo.getPointerInfo().getLocation() (see Get Mouse Position ). 因此,您必须使用MouseInfo.getPointerInfo()。getLocation()之类的东西在外部找到光标位置(请参阅获取鼠标位置 )。

Then, to convert that coordinate to be relative to the jPanel, you can use SwingUtilities.convertPointFromScreen(). 然后,要将该坐标转换为相对于jPanel的坐标,可以使用SwingUtilities.convertPointFromScreen()。

Here, I have updated the cursor position in an overridden updateBuffer function which then calls its parent to draw everything. 在这里,我已经在覆盖的updateBuffer函数中更新了光标位置,然后该函数调用其父项以绘制所有内容。 Note that, I only update the hover position when the scrollbar is being dragged (eg 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. 请注意,我仅在拖动滚动条时更新悬停位置(例如,areLabelsBeingScrolled()),它仅返回一个布尔值,该布尔值在鼠标单击滚动条时设置为true,在释放滚动条时设置为false。

@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 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. 此外,为了使光标悬停更改平滑,我使用了一个计时器,该计时器使updateBuffer在滚动期间不断更新。 But that is ancillary to the question, so I won't go into further detail about it. 但这是该问题的辅助条件,因此我将不做进一步的详细介绍。

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

From this code you can get position of cursor when it is pressed on a label. 通过此代码,您可以将光标在标签上按下时的位置。

I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM