【问题标题】:How to change content of a cell in a Table when clicking on that cell单击该单元格时如何更改表格中单元格的内容
【发布时间】:2018-09-20 18:46:02
【问题描述】:

我有一个关于 Java SWT 的表。我有一个“已付款”列,所有项目都写有“未付款”和未付款图标。这是表格的第四列。

我已经用虚拟内容填充了表格,现在,我想在第四列“付费”的单元格上添加一个侦听器,以更改单击单元格的内容。如果单元格有非付费文字和图标,必须改为付费文字和图标,反之亦然。

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

这是我的源代码:

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

【问题讨论】:

  • 为什么不使用第四列的按钮来更改已付款状态? (也许我误解了用例)我认为这将涵盖您的情况,并且更容易添加适当的侦听器。
  • @RafaelPalomino 我该如何使用按钮?我可以放一个复选框而不是一个按钮吗?谁会更好地满足我的需求。您可以使用复选框和侦听器共享示例代码吗?谢谢
  • ok @RafaelPalomino 我设法添加了一个带有复选框样式的按钮,现在我可以添加一个侦听器,但是我怎么知道在哪一行单击了复选框?

标签: java eclipse swt windowbuilder


【解决方案1】:

由于您在创建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));
        }
    });
}

编辑: 如 cmets 中所述,如果删除了一行,则使用 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));
}

【讨论】:

  • 如何获取实际的TableItem?
  • @NullPointerException 在for 循环开始处创建的TableItem 已经可以从widgetSelected 中访问。更新了我的答案,以显示访问项目并根据您的问题获取第三列中的值的示例。
  • 还有另一种方法可以知道用户按下了哪个按钮?我问它是因为删除行的情况很常见,所以您使用的索引可能不正确。我尝试使用“membersTable.getSelectionIndex();”在 widgetSelected 事件上,但它返回 -1,因为当您按下按钮时,您没有按下行
  • 尽管index 在这种情况下不可靠,但使用tableItem 会是可靠的。如果您需要该项目的数字索引,您可以使用侦听器内部的Table#indexOf(TableItem) 方法。将更新我的答案。
  • @NullPointerException 不客气 - 很高兴我能帮上忙!
猜你喜欢
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
相关资源
最近更新 更多