Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the MouseClick event on an SWT Button?

Tags:

java

button

swt

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?

like image 211
Heinzi Avatar asked Jul 14 '14 14:07

Heinzi


2 Answers

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.

like image 157
Baz Avatar answered Oct 30 '22 02:10

Baz


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

like image 36
Mister Avatar answered Oct 30 '22 01:10

Mister