问题
I am very new to SWT. Started working on it today actually. I have a table of type CheckboxTableViewer. What i want to be able to do is whenever the user selects the row (i.e clicks anywhere on the row) I want the check box to be checked (ticked). Currently I have a listener on the CheckboxTableViewer as follows:
diagnosesTableViewer.addCheckStateListener(new ICheckStateListener() {
@Override
public void checkStateChanged(CheckStateChangedEvent event) {
Nomenclature changedStateNomenclature = (Nomenclature) event
.getElement();
if (event.getChecked()) {
selectedNomenclatures.add(changedStateNomenclature);
} else {
selectedNomenclatures.remove(changedStateNomenclature);
}
}
});
I am able to select the row by checking on the checkbox. But i want to select the check box even when the user selects the row by clicking anywhere on that row on any column (not just the checkbox).
I guess that logic would go somewhere in the addSelectionChangedListener
for the addSelectionChangedListener. But I am not sure how to go about it. Can anyone help me with this?
回答1:
Use this code: Add selection listener to the table. ctv
is the instance of of your CheckboxTableViewer
.
Also I assumed CheckboxTableViewer
allow only single selection not multi.
ctv.getTable().addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int df = ctv.getTable().getSelectionIndex();
ctv.setChecked(ctv.getElementAt(df), !ctv.getChecked(ctv.getElementAt(df)));
}
});
来源:https://stackoverflow.com/questions/26267230/set-the-checkbox-of-a-checkboxtableviewer-when-the-row-is-clicked