Fire an event on mouseClick in SWT?
Asked Answered
Y

2

4

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.

Youth answered 11/1, 2012 at 22:54 Comment(3)
What kind of object? Just a Composite? (Most of the native wrapper types have SelectionListeners to implement this, but otherwise mouse down and mouse up are unambiguous. "Click" less so.)Thickening
I am drawing an image using GC(GraphicsContext) How to fire an event when the user clicks on the image ?Youth
You write "How to fire an event based on the user click?" What do you mean? The SWT listeners listens for events from the display...Ocrea
B
2

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.

Barren answered 12/1, 2012 at 7:8 Comment(2)
I am not getting the context where the mouseClick() event will be useless. Can you be a bit more explanative ?Youth
@ynwa Updated my answer. This is just theoretical and doesn't change the fact that there is no MouseClick event in SWT :)Doralynne
T
0

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!");
        }
    });
Tresatrescha answered 2/3, 2017 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.