nik7
nik7

Reputation: 3058

Fire an event on mouseClick in SWT?

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.

Upvotes: 4

Views: 2983

Answers (2)

stuhpa
stuhpa

Reputation: 306

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!");
        }
    });

Upvotes: 0

Martti Käärik
Martti Käärik

Reputation: 3621

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.

Upvotes: 2

Related Questions