beld
beld

Reputation: 81

SWT TableItem getText doesn't return what I expect

I want to swap two texts in TableItems. Firstly, I set the text, then I check which TableItems are selected, save them in 2 variables and overwrite them. But I get these strings instead of the message I wanted:

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

The part after the @ is always different, I guess it's an ID or something but I can't find a solution. Here's the code snippets. groupsList is a String array.

    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);
    }

So I wrote the content into the TableItems, then I want to swap them:

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);
        }
    });

Here's the GUIenter image description here

Upvotes: 0

Views: 298

Answers (1)

avojak
avojak

Reputation: 2360

Take a look these lines in your MouseAdapter:

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

Notice that Table.getSelection() returns an array of TableItem objects. As @greg-449 pointed out, you'll get [Lorg.eclipse.swt.widgets.TableItem;@XXXXXXXX if you call toString() on that array.

In each of those two cases you've already checked that there is only one TableItem selected, so you can safely do table.getSelection()[0] to access that TableItem (Alternatively, you could do table.getItem(table.getSelectionIndex()) after verifying that there is at least one and only one item selected).

In an unrelated if-statement later on, you're correctly getting the TableItem text:

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

So instead of using the toString() method on those two lines at the start, you'll want to use getText() as you've done here.

Upvotes: 1

Related Questions