Reputation: 2027
Hi I am relatively new to SWT. I am using a CheckboxTableViewer. I have a listener on it:
diagnosesTableViewer.getTable().addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
int nomenclatureSelectionIndex = diagnosesTableViewer
.getTable().getSelectionIndex();
Nomenclature checkedNomenclature = (Nomenclature) diagnosesTableViewer
.getElementAt(nomenclatureSelectionIndex);
diagnosesTableViewer.setChecked(checkedNomenclature,
!diagnosesTableViewer
.getChecked(checkedNomenclature));
}
When I click on the table row (anywhere but the checkbox), everything works as expected. But when I click on the checkbox specifically, I unchecks the checkbox of the previously selected row as well. I think it is because when I click on the checkbox, it does not change the focus to the new row and hence the listener is called on the old, previously selected row itlsef and the new row whose checkbox I am selecting, its listener is never being called. How can I solve this?
Upvotes: 0
Views: 425
Reputation: 111142
Use CheckboxTableViewer.addCheckStateListener
to add an ICheckStateListener
to deal with check boxes being clicked.
viewer.addCheckStateListener(new ICheckStateListener() {
@Override
public void checkStateChanged(CheckStateChangedEvent event) {
// event.getChecked() is the check state
// event.getElement() is element being checked/unchecked
}
});
You can select the clicked row in the event with
viewer.setSelection(new StructuredSelection(event.getElement()));
Use
IStructuredSelection selection = (IStructuredSelection)viewer.getSelection();
to get the existing selection.
Upvotes: 2