单击行时设置CheckboxTableViewer的复选框 [英] Set the checkbox of a CheckboxTableViewer when the row is clicked

查看:1175
本文介绍了单击行时设置CheckboxTableViewer的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SWT很新。今天开始工作。我有一个类型的表CheckboxTableViewer。我想要能够做的是每当用户选择行(即点击行上的任何地方)我想要复选框被选中(勾选)。目前我在CheckboxTableViewer上有一个监听器,如下所示:

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).

我猜逻辑将在addSelectionChangedListener的 addSelectionChangedListener 中的某处。但我不知道如何去。任何人都可以帮助我这个?

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?

推荐答案

使用此代码:添加选择监听器到表。 ctv 是您的 CheckboxTableViewer 的实例。

Use this code: Add selection listener to the table. ctv is the instance of of your CheckboxTableViewer.

此外,我假设 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)));
  }         
});

这篇关于单击行时设置CheckboxTableViewer的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆