Bug 577860 - SelectionListener is not called when invoking setSelection on Checkbox Button
Summary: SelectionListener is not called when invoking setSelection on Checkbox Button
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.22   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-12-17 02:32 EST by Alois Zoitl CLA
Modified: 2022-01-09 12:11 EST (History)
4 users (show)

See Also:


Attachments
Snippet showing the problem (1.28 KB, text/x-java)
2021-12-17 02:32 EST, Alois Zoitl CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
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.