简体   繁体   中英

Where is the MouseClick event on an SWT Button?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM