2017-04-12 64 views
0

我想交換TableItem s中的兩個文本。首先,我設置文本,然後檢查選擇哪個TableItem,將它們保存在2個變量中並覆蓋它們。但我得到這些字符串我想要的信息,而不是:SWT TableItem getText不會返回我期望的結果

[Lorg.eclipse.swt.widgets.TableItem;@6fadae5d

的部分@後始終是不同的,我想這是一個ID或東西,但我不能找到一個解決方案。這裏是代碼片段。 groupsListString陣列。

for (int i = 1; i <= logic.amountOfGroups; i++) { 

     Table table = new Table(shell, SWT.MULTI | SWT.BORDER); 
     table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1)); 
     for (int j = 0; j < logic.personsInGroup; j++) { 
      TableItem tableItem_1 = new TableItem(table, SWT.NONE); 
      tableItem_1.setText(logic.groupsList.get(i - 1)[j]); 
     } 
     tableList.add(table); 
    } 

所以我寫的內容納入TableItems,那麼我想交換他們:

swapButton = new Button(shell, SWT.NONE); 
    swapButton.setText("Swap"); 
    swapButton.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseDown(MouseEvent e) { 
      int[] playerIndices = new int[2]; 
      int[] groupIndices = new int[2]; 
      int i = 0; 
      String toBeSwappedZero = ""; 
      String toBeSwappedOne = ""; 
      for (Table table : tableList) { 
       if (table.getSelectionCount() == 1) { 
        if (toBeSwappedZero == "") { 
         groupIndices[0] = i; 
         playerIndices[0] = table.getSelectionIndex(); 
         toBeSwappedZero = table.getSelection().toString(); 
        } else { 
         groupIndices[1] = i; 
         playerIndices[1] = table.getSelectionIndex(); 
         toBeSwappedOne = table.getSelection().toString(); 
        } 
       } 
       if (table.getSelectionCount() == 2) { 
        playerIndices = table.getSelectionIndices(); 
        groupIndices[0] = i; 
        groupIndices[1] = i; 
        toBeSwappedZero = table.getItem(playerIndices[0]).getText(); 
        toBeSwappedOne = table.getItem(playerIndices[1]).getText(); 
       } 
       i++; 
      } 
      System.out.println(toBeSwappedOne); 
      tableList.get(groupIndices[0]).getItem(playerIndices[0]).setText(toBeSwappedOne); 
      tableList.get(groupIndices[1]).getItem(playerIndices[1]).setText(toBeSwappedZero); 
     } 
    }); 

這裏的GUI enter image description here

+0

所有代碼的哪個部分給出了這個結果? '[Lorg.eclipse.swt.widgets.TableItem; @ 6fadae5d'是通過調用表項數組上的'toString'得到的。 –

回答

1

採取看看這些行你MouseAdapter

if (table.getSelectionCount() == 1) { 
    if (toBeSwappedZero == "") { 
     // ... 
     toBeSwappedZero = table.getSelection().toString(); 
    } else { 
     // ... 
     toBeSwappedOne = table.getSelection().toString(); 
    } 
} 

請注意,Table.getSelection()返回一個的數組對象。正如@ greg-449指出的那樣,如果您在該陣列上調用toString(),您將獲得[Lorg.eclipse.swt.widgets.TableItem;@XXXXXXXX

在每一個你已經選中這兩個案例,只有一個選擇TableItem,所以你可以放心地做table.getSelection()[0]訪問TableItem(或者,你可以驗證至少有一個後做table.getItem(table.getSelectionIndex())和只有一個項目被選中)

在一個不相關的if語句來以後,你正確地獲取TableItem文本:

table.getItem(playerIndices[0]).getText(); 

因此,而不是使用toString()方法上,在開始的兩行的,你要使用就像你在這裏完成的那樣。

+0

謝謝,現在無法測試,但這可能是。 – beld

+0

沒問題 - 讓我知道如果它出於某種原因不起作用,我很樂意再看一次。 – avojak