Set the checkbox of a CheckboxTableViewer when the row is clicked

百般思念 提交于 2019-12-24 22:17:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!