Bug 577860

Summary: SelectionListener is not called when invoking setSelection on Checkbox Button
Product: [Eclipse Project] Platform Reporter: Alois Zoitl <alois.zoitl>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: Lars.Vogel, niraj.modi, rolf.theunissen, twolf
Version: 4.22   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=578103
Whiteboard:
Attachments:
Description Flags
Snippet showing the problem none

Description Alois Zoitl CLA 2021-12-17 02:32:47 EST
Created attachment 287701 [details]
Snippet showing the problem

As identified in Bug #577857 when setting the selection of a checkbox button programmatically via setSelection() the selection listener is not called.  PFA a snippet showing the problem.
Comment 1 Lars Vogel CLA 2021-12-17 02:51:42 EST
You may be right Alois, with Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=577857#c3 that this is on purpose. The Javadoc says the selection listener is called when the user selects the button.
Comment 2 Lars Vogel CLA 2021-12-17 02:52:24 EST
Niraj, WDYT? Should be selectionListener be called if we call setSelection on a Button?
Comment 3 Alois Zoitl CLA 2021-12-17 03:07:07 EST
Was to quick with pressing the submit button. What I forgot to write is that I'm not sure if it is SWT bug or not. Not having the selection listener called has also advantages.
Comment 4 Thomas Wolf CLA 2021-12-19 13:29:20 EST
AFAIK this is by design. Same behavior also exists in tables or trees. If you programmatically set the selection, your program knows what it set the selection to, so it can perform additional work explicitly. Triggering a listener may in fact complicate matters, and most programmatic uses would have to temporarily disable such listeners.

Consider a form with several fields, and a selection listener that triggers form validation. When you're initializing the form programmatically, you probably don't want that listener to be triggered on setSelection() -- it might mark the form input as invalid unless you had set other fields in the form to consistent values, too.

However, when the selection changes due to triggers outside your code, such as user interaction, then you want and in fact need listeners to be invoked.
Comment 5 Rolf Theunissen CLA 2022-01-07 03:20:07 EST
Similar questions have been asked years ago:
https://stackoverflow.com/questions/9102026/programmatically-fire-a-rcp-selection-event

Though there are also questions that ask how to disable the firing of selection changed (on structured viewers):
https://stackoverflow.com/questions/24245554/how-to-set-selection-for-treeviewer-without-firing-selectionchanged

For structured viewers there seems to be a difference when a control has focus or not, which could be another bug.