2

我构建了一个表格(org.eclipse.swt.widgets.Table)并使用 TableEditor 在每个 TableItem(行)上添加 2 个复选框。

TableItem tableItem = new TableItem(tableClientes, SWT.NONE);
TableEditor editor = new TableEditor(tableClientes);

Button checkEnviar = new Button(tableClientes, SWT.CHECK);
checkEnviar.pack();

editor.minimumWidth = checkEnviar.getSize().x + 5;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(checkEnviar, tableItem, 0);

editor = new TableEditor(tableClientes);

Button checkMarcar = new Button(tableClientes, SWT.CHECK);
checkMarcar.pack();

editor.minimumWidth = checkMarcar.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor(checkMarcar, tableItem, 1);

现在我需要读取每一行上每个复选框的状态,但我不知道如何。

我尝试以这种方式检索 TableItem:

tableClientes.getItem(x)

但在它的内容中,我无法检索复选框状态或对其进行操作。任何想法如何做到这一点?

4

1 回答 1

2

一个简单的解决方案是使用类setData()的方法Widget。例如看下面的代码(特别是最后一个按钮的选择监听器,但要测试一个有效的小部件实例):

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableItemTest {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        shell.setLayoutData(gridData);

        final Table table = new Table(shell, SWT.BORDER);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        table.setBounds(new org.eclipse.swt.graphics.Rectangle(47,67,190,70));

        TableColumn tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setWidth(180);
        tableColumn.setText("Check Column"); 

        for(int i=0; i<10; i++){
            Button checkButton = new Button(table, SWT.CHECK); 
            checkButton.pack();

            TableItem tableItem=new TableItem(table,SWT.NONE);
            TableEditor editor = new TableEditor (table);

            editor.minimumWidth = checkButton.getSize ().x;
            editor.horizontalAlignment = SWT.CENTER;
            editor.setEditor(checkButton, tableItem, 0);
            tableItem.setData("CHECK_BOX", checkButton); //Point to notice
        }

        Button checkState = new Button(shell, SWT.PUSH);
        checkState.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        checkState.setText("Check State");
        checkState.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                TableItem[] items = table.getItems();
                for (int i = 0; i < items.length; i++) {
                    Object data = items[i].getData("CHECK_BOX");
                    if(data != null){
                        Button checkButton = (Button)data;
                        if(!checkButton.isDisposed())
                            System.out.println("For row " + (i+1) + " check status is " + checkButton.getSelection());
                    }
                }
            }
        });

        shell.setSize(220, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
于 2013-04-24T03:59:57.570 回答