Fire an event on mouseClick in SWT?

北城以北 提交于 2019-12-07 10:54:46

问题


In SWT, for MouseListener interface, the methods available are mouseUp(), mouseDown() and mouseDoubleClick()

How to fire an event based on the user click?

We can do this by conjunction of mouseUp() and mouseDown() but isn't there any trivial solution like mouseClick() method in SWT?

Thanks.


回答1:


How would a mouse-click event be defined? Mouse-down followed by mouse-up without mouse leaving the bounds of the control (otherwise it would be drag-start), right? By that definition, mouse-click event couldn't be associated with a single point, but rather with an area (1) or a control (2). The first case wouldn't fit into generic SWT event, which only has a location (x and y), and you'd still need additional code to check whether the click area was inside your image. In the second case where the mouse-click would only be defined using a control (and no location), the event would be useless to you.

When you have implemented your own single-click detection you can fire any events on the control you like, even those not defined by SWT.




回答2:


To react on a mouse click event on a button you can use SelectionListener, something like this should do the trick:

button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            System.out.println("click!");
        }
    });


来源:https://stackoverflow.com/questions/8827929/fire-an-event-on-mouseclick-in-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!