代码之家  ›  专栏  ›  技术社区  ›  NullPointerException

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

  •  0
  • NullPointerException  · 技术社区  · 6 年前

    我有一张关于javaswt的桌子。我有一个“付款”列,所有的项目都写了“非付款”和非付款图标。这是表格的第四列。

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

    这是我的源代码:

    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?
                }
            });
        }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   avojak    6 年前

    既然你在里面 for 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));
    }