【问题标题】:SWT - TableViewer - Refreshing SelectionSWT - TableViewer - 刷新选择
【发布时间】:2012-09-17 18:24:30
【问题描述】:

我有一个运行方法的按钮。该方法获取表中选定的行并将它们添加到数组列表中。这在第一次执行时效果很好。但如果用户选择了错误的行,他们将能够重新选择不同的行并将该选择数据添加到 arraylist。

但是对于我当前的代码,用户第二次选择哪一行并不重要,第一次选择的数据总是被添加到数组列表中。这就像在用户选择新行之前需要重置或刷新选择。

按钮代码

Button pdfButton = new Button(composite, SWT.PUSH);
   pdfButton.setText("Get Plotter List");
   pdfButton.setEnabled(true);
   pdfButton.addSelectionListener(new SelectionAdapter() {
       public void widgetSelected(SelectionEvent e) {
          getPlotterSelection();
       }
   }); 

方法代码

 public void getPlotterSelection() {
    selectedPlotters.clear(); <-- Clearing the ArrayList
    int[] row = viewer.getTable().getSelectionIndices(); <-- Getting Current Selections
    Arrays.sort(row);

    if (row.length > 0) {
       for(int i = row.length-1; i >= 0; i--){
          PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
          selectedPlotters.add(pp);
        }
     }
     viewer.getTable().deselectAll();
   }

在我写这篇文章时,我认为问题可能出在 getSelectionIndices() 中。似乎是选择的行数,而不是实际的行数

编辑

问题出在我的逻辑上。我得到了正确的索引,但是在 for 循环中使用 i 变量来获取值。

for(int i = row.length-1; i >= 0; i--){
          PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());

改成

aa.get(row[i].getPrinterName(), etc...

它就像我想象的那样工作

【问题讨论】:

  • 我不明白你的问题。您能否添加更多细节?正如您在JavaDoc 中看到的那样,getSelectionIndices() 确实返回了索引。什么是错行

标签: swt tableviewer


【解决方案1】:

既然您已经在使用TableViewer,为什么不从中选择呢?

IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
YourObject[] array = (YourObject[])selection.toArray();

然后您可以遍历数组并将它们添加到您的ArrayList

【讨论】:

  • @jkteater 如果你喜欢它,你可能想要投票。之后,您可以自行创建一个答案,其中包含对您问题的修改并接受它。
  • 我更改了代码以符合您的建议。所以我认为你应该得到答案
  • @jkteater 谢谢。非常慷慨。
猜你喜欢
  • 1970-01-01
  • 2013-02-06
  • 2011-12-20
  • 2015-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
相关资源
最近更新 更多