单击单元格时如何更改 Table 中单元格的内容

How to change content of a cell in a Table when clicking on that cell

我在 Java SWT 上有一个 Table。我有一个 "payed" 列,所有项目都写有 "non payed" 和一个未付费的图标。这是 table.

的第四列

我已经用虚拟内容填充了 table,现在,我想在第四列的单元格上添加一个侦听器 "payed" 以更改单击的单元格的内容。如果单元格有非付费文本和图标,必须更改为付费文本和图标,以及相反的行为。

我找不到实现它的方法,因为我无法将选择侦听器添加到 table 的单元格,或者我不知道该怎么做。

这是我的源代码:

Table membersTable = new Table(clubComposite, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
membersTable.setLinesVisible(true);
membersTable.setHeaderVisible(true);
membersTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

TableColumn tblclmnName = new TableColumn(membersTable, SWT.NONE);
tblclmnName.setWidth(150);
tblclmnName.setText("Nombre");

TableColumn tblclmnCommonPhoneNumber = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonPhoneNumber.setWidth(120);
tblclmnCommonPhoneNumber.setText("Teléfono");

TableColumn tblclmnCommonMoney = new TableColumn(membersTable, SWT.NONE);
tblclmnCommonMoney.setWidth(150);
tblclmnCommonMoney.setText("Participación Habitual");

TableColumn tblclmnPayed = new TableColumn(membersTable, SWT.CENTER);
tblclmnPayed.setWidth(50);
tblclmnPayed.setText("Payed"); 

// populate Table
for (int i=0; i<50; i++) {
    TableItem tableItem = new TableItem(membersTable, SWT.CENTER);                  
    tableItem.setText(new String[] {"person "+i, "610610620", "100", "non payed"});
    tableItem.setImage(3, uncheckedImage);
}

编辑:

好的,我设法用一个复选框完成了,但现在我怎么知道在哪一行点击了复选框?我需要它来获取第三列的值

// populate Table
    for (int i=0; i<50; i++) {
        TableItem tableItem = new TableItem(membersTable, SWT.NONE);                    
        tableItem.setText(new String[] {"person "+i, "610610620", "100"});

        Button button = new Button(membersTable, SWT.CHECK);
        button.pack();
        TableEditor editor = new TableEditor(membersTable);
        editor.minimumWidth = button.getSize().x;
        editor.horizontalAlignment = SWT.CENTER;
        editor.setEditor(button, tableItem, 3);

        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                //how to know in which row is clicked the checkbox?
            }
        });
    }

由于您在创建 TableItemfor 循环中,您已经有了该行的索引。同样,如果您需要实际的 TableItem,您也已经可以访问它了。

for (int i=0; i<50; i++) {
    TableItem tableItem = new TableItem(membersTable, SWT.NONE);                    
    tableItem.setText(new String[] {"person "+i, "610610620", "100"});

    Button button = new Button(membersTable, SWT.CHECK);
    button.pack();
    TableEditor editor = new TableEditor(membersTable);
    editor.minimumWidth = button.getSize().x;
    editor.horizontalAlignment = SWT.CENTER;
    editor.setEditor(button, tableItem, 3);

    final int index = i; // Hold the row index here

    button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            //how to know in which row is clicked the checkbox?
            System.out.println("Row selected: " + index);
            System.out.println("Participación Habitual: " + tableItem.getText(2));
        }
    });
}

编辑: 如评论中所述,如果删除一行,使用 index 将不可靠。相反,如果您需要数字索引,可以使用 Table.indexOf(TableItem)

@Override
public void widgetSelected(SelectionEvent e) {
    //how to know in which row is clicked the checkbox?
    System.out.println("Row selected: " + membersTable.indexOf(tableItem));
    System.out.println("Participación Habitual: " + tableItem.getText(2));
}