Where is the MouseClick event on an SWT Button?
Asked Answered
D

2

9

I know this sounds like a very basic question, and I feel embarrassed to ask it but...

How to I add a Mouse Click handler to an SWT button?

What I checked:

  • I can find tons of examples on how to add mouse down, mouse up or double click handlers (by assigning a MouseListener). Obviously, mouse click (sequence of down and up on the same control) is something different than mouse down.
  • I understand that there might not be a click handler on generic controls, but the only event I see added on the Button control is a SelectionListener -- that might be it, but selection sounds more like "received focus" to me than "was clicked or selected and then invoked with a key press".
  • I found a related question, whose answer basically says that you need to implement that yourself -- I find that a bit hard to believe.

Is selection what is commonly known as "OnClick" in other languages/frameworks? Or is there something else that I completely missed?

Devise answered 14/7, 2014 at 14:12 Comment(2)
Yes, SWT.Selection or SelectionListener is what you're looking for. It might be called selection, because a Button can be a checkbox or a radion button depending on it's style.Landau
@Baz: Great, thanks. Make that an answer and I will accept it -- hopefully it will help others in the future.Devise
L
7

Yes, SWT.Selection or SelectionListener is what you're looking for:

Button button = new Button(shell, SWT.PUSH);
button.addListener(SWT.Selection, new Listener()
{
    @Override
    public void handleEvent(Event event)
    {
        System.out.println("SWT.Selection");
    }
});

adding a SelectionListener internally does the same as the code above.

It might be called selection, because a Button can be a checkbox or a radion button depending on it's style.

Landau answered 14/7, 2014 at 14:16 Comment(0)
P
1

Yes SelectionListener is what you are after. I, too, am more of a fan of the OnClick terminology myself as it is more cut and dry; I digress.

Here is a good example for you to see how it works: http://www.java2s.com/Tutorial/Java/0280__SWT/UsingSelectionListener.htm

Petition answered 14/7, 2014 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.