Search code examples
javaswtlistener

Understanding the widgetSelected event and the widgetDefaultSelected event in the swt SelectionListener?


I'm having trouble understanding the difference between the 2 methods in the SWT SelectionListener. The javadoc is as follows:

void org.eclipse.swt.events.SelectionListener.widgetSelected(SelectionEvent e)

Sent when selection occurs in the control.

For example, selection occurs in a List when the user selects an item or items with the keyboard or mouse. On some platforms, the event occurs when a mouse button or key is pressed. On others, it happens when the mouse or key is released. The exact key or mouse gesture that causes this event is platform specific.

void org.eclipse.swt.events.SelectionListener.widgetDefaultSelected(SelectionEvent e)

Sent when default selection occurs in the control.

For example, on some platforms default selection occurs in a List when the user double-clicks an item or types return in a Text. On some platforms, the event occurs when a mouse button or key is pressed. On others, it happens when the mouse or key is released. The exact key or mouse gesture that causes this event is platform specific.

It sounds to me like widgetSelected() is called when the user selects a widget in any way. The widgetDefaultSelected() is called when the user is finished interacting with the widget. For a Text widget, that would be pressing Enter; for a List, that would be double clicking on a list item; and for a Date, that would be pressing Enter.

Is this understanding correct?


Solution

  • Your general understanding is correct, though the term 'finished' might not be 100% accurate in all situations.

    Widgets that send (default) selection events document the particular details in the JavaDoc of their respective addSelectionListener method. There you can read if and when widgetSelected and/or widgetDefaultSelected is sent.

    If you look into the Link::addSelectionListener JavaDoc for example, you will see that widgetDefaultSelected() is never called.