English 中文(简体)
Mouse events on an SWT Scrollbar
原标题:

Using standalone SWT Scrollbars is something of a hack (using this workaround), but it can be done. 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.

Word-style scrolling tooltip

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.

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:

When widgetSelected is called, the event object detail field contains one of the following values: SWT.NONE - for the end of a drag. SWT.DRAG. SWT.HOME. SWT.END. SWT.ARROW_DOWN. SWT.ARROW_UP. SWT.PAGE_DOWN. SWT.PAGE_UP. widgetDefaultSelected is not called.

So my suggestion is get your tooltip to appear and disappear is to check for the event.detail type.

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. 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!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签