2012-09-17 50 views
1

我有一個運行方法的按鈕。該方法獲取表中選定的行並將它們添加到數組列表中。這在第一次執行時效果很好。但是,如果用戶選擇了錯誤的行,他們將能夠重新選擇不同的行並將該選擇數據添加到數組列表。SWT - TableViewer - 刷新選擇

但是對於我目前的代碼,用戶選擇第二次選擇的第一個數據總是添加到數組列表中時,哪一行都沒有關係。這就好像選擇需要在用戶選擇新行之前重置或刷新。

按鈕代碼

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循環中使用變量來獲取值。

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

和它就像我認爲這將

+0

我不明白你的問題。你可能會添加更多的細節?正如你可以在[JavaDoc]中看到的(http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets %2FTable.html),'getSelectionIndices()'確實返回索引。什麼是_wrong row_? – Baz

回答

1

既然你已經使用了TableViewer,爲什麼不從它那裏得到的選擇?

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

然後,您可以迭代數組並將它們添加到您的ArrayList

+0

我真的很喜歡 – jkteater

+0

@jkteater如果你喜歡它,你可能想要upvote。之後,您可以自行創建一個包含您的問題編輯的答案並接受它。 – Baz

+0

我更改了我的代碼以符合您的建議。所以我認爲你應該得到答案 – jkteater