简体   繁体   English

SWT滚动条上的鼠标事件

[英]Mouse events on an SWT Scrollbar

Using standalone SWT Scrollbars is something of a hack (using this workaround ), but it can be done. 使用独立的SWT滚动条是一种技巧(使用此替代方法 ),但是可以做到。 Here's a snippet: 这是一个片段:

ScrolledComposite scrolledComposite = new ScrolledComposite(
                parent, SWT.V_SCROLL);
ScrollBar scrollbar = scrolledComposite.getVerticalBar();
Shell tip = new Shell(UserInterface.getShell(), SWT.ON_TOP
                | SWT.NO_FOCUS | SWT.TOOL);
// ..stylize and fill the tooltip..

Now what I'm trying to do is monitor when the user is interacting with the scrollbar. 现在,我要尝试的是监视用户与滚动条进行交互的时间。 In particular, I want to know when the user is dragging the scrollbar—and when it has been released—in order to display an Office 2007-style tooltip revealing which page the position of the scrollbar corresponds with. 特别是,我想知道用户何时拖动滚动条以及何时释放滚动条,以便显示Office 2007样式的工具提示,以显示滚动条的位置与哪个页面相对应。

文字样式滚动工具提示

Presently, I have the following code which displays the tooltip: 目前,我有以下代码显示工具提示:

scrollbar.addSelectionListener(new SelectionListener() {
    public void widgetDefaultSelected(SelectionEvent event) {}
    public void widgetSelected(SelectionEvent event) {
        tip.setVisible(true);
    }
}

It would seem logical then to have the tooltip disappear when the mouse button is released: 松开鼠标按钮时,使工具提示消失似乎是合乎逻辑的:

scrollbar.addListener(SWT.MouseUp, new Listener() {
    public void handleEvent(Event event) {
        tip.setVisible(false);
    }
});

However, neither scrollbar nor scrolledComposite seem to respond to the SWT.MouseUp event when the user interacts with the scrollbar. 然而,无论是scrollbarscrolledComposite似乎在回应SWT.MouseUp当用户与滚动条交互事件。

I presently have a workaround that hides the tip after a timeout, but I'm not satisfied with this. 我目前有一种解决方法,可以在超时后隐藏提示,但对此我并不满意。 Any insights would be most appreciated! 任何见解将不胜感激!

Scrollbar's javadoc said this: 滚动条的javadoc这样说:

When widgetSelected is called, the event object detail field contains one of the following values: SWT.NONE - for the end of a drag. 调用widgetSelected时,事件对象详细信息字段包含以下值之一:SWT.NONE-拖动结束。 SWT.DRAG. SWT.DRAG。 SWT.HOME. SWT.HOME。 SWT.END. SWT.END。 SWT.ARROW_DOWN. SWT.ARROW_DOWN。 SWT.ARROW_UP. SWT.ARROW_UP。 SWT.PAGE_DOWN. SWT.PAGE_DOWN。 SWT.PAGE_UP. SWT.PAGE_UP。 widgetDefaultSelected is not called. 没有调用widgetDefaultSelected。

So my suggestion is get your tooltip to appear and disappear is to check for the event.detail type. 因此,我的建议是让您的工具提示出现和消失是检查event.detail类型。

public void widgetSelected(SelectionEvent event) {
    tip.setVisible(event.detail != SWT.NONE);
}
scrollBar.addSelectionListener(new SelectionListener() {
    public void widgetDefaultSelected(SelectionEvent e) {
    }

    public void widgetSelected(SelectionEvent e) {
        if (e.detail == SWT.NONE) {
            // end of drag
            System.out.println("Drag end");
        }
        else if (e.detail == SWT.DRAG) {
            // drag
            System.out.println("Currently dragging");
        }
    }
});

Hope this will help you... But I can see a problem with mousewheel use that throws multiple drag end events... 希望这会对您有所帮助...但是我可以看到使用鼠标滚轮时会引发多个拖动结束事件的问题...

Paul, 保罗

try using addMouseEvent method from a Scrollable object. 尝试从Scrollable对象使用addMouseEvent方法。 For example: 例如:

Scrollable scrollable = scrollbar.getParent();
scrollable.addMouseListener(new MouseListener () {
   void mouseDoubleClick(MouseEvent e) { ... }
   void mouseDown(MouseEvent e) { ... }
   void mouseUp(MouseEvent e)  { ... }
});

Actually, I don't know if this approach will work. 其实,我不知道这种方法是否行得通。 But, it's an attempt. 但是,这是一种尝试。 Good luck! 祝好运!

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

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