【问题标题】:Eclipse Table widget with SWT.CHECK is fireing two events when a TableItem is selected选择表项时,带有 SWT.CHECK 的 Eclipse 表小部件会触发两个事件
【发布时间】:2013-04-24 02:26:15
【问题描述】:

我正在制作一个使用我自己的向导页面(扩展 MBSCustomPage)的 Eclipse (3.6.2) CDT (7.0.2) 插件。此向导页面显示了一个表格,其中包含一些可以通过单击它们来选中或取消选中的 TableItems(像往常一样)。 问题是当 TableItem 复选框被选中(或未选中)时,我总是收到两个事件!。在收到的第一个事件中,我有一个 (SelectedEvent)e.detail == SWT.CHECK,即使首先检查了 TableItem,在第二个事件中,(SelectedEvent)e.detail == 0!。所以我无法知道 TableItem 是否真的被检查过。 这是我的代码(有点):

final Table table = new Table( composite, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION | SWT.CHECK );
table.setHeaderVisible(true);
table.setLinesVisible(false);

(...)

table.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
        CheckThatOnlyOneItemCanBeCheckedAtTime(e.item);
        //If someone check on me, save the item data value in a "container"
        if( e.detail == SWT.CHECK ) {
            MBSCustomPageManager.addPageProperty(PAGE_ID, "SDK_Path", ((ISdk)((TableItem)e.item).getData()).getPath() );
        } else { //Otherwise, unset the saved value
            MBSCustomPageManager.addPageProperty(PAGE_ID, "SDK_Path", "" );
        }
    }
});

为什么当我单击 TableItem 的复选框时,widgetSelected() 会被调用两次? 我测试了即使在 widgetSelected() 方法中没有代码也会触发事件。 我没有在 Eclipse Bugzilla 数据库中搜索或查找任何东西……对我来说真的很奇怪,但我不是经验丰富的 Eclipse 插件编码器(甚至不是 Java):)

谢谢!

【问题讨论】:

    标签: java events eclipse-plugin eclipse-cdt


    【解决方案1】:
    table.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event arg0) {
        String string = arg0.detail == SWT.CHECK ? "Checked" : "Selected";
        if (arg0.detail == SWT.CHECK) {
          System.out.println(arg0.item + " " + string+ ":" + 
                             ((Table)arg0.widget).getSelection()[0].getChecked());
         }                         
         else {
           System.out.println(arg0.item + " " + string);
         }
       }
    });
    

    【讨论】:

    • 使用 ((TableItem)arg0.item).getChecked() 代替。上面的代码可能无法解析正在检查的项目,因为它会查找第一个选定的项目(可能是也可能不是正在检查的项目)。它将给出一个空指针异常(NPE),即没有选择表项(并且不需要选择任何项来选中/取消选中项)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 2018-03-05
    • 2021-02-05
    相关资源
    最近更新 更多