如何在 JFace 的 TableViewer 中创建复选框?

How can I create a checkbox in a TableViewer of JFace?

我创建了一个包含两列的 tableViewer,我想将其中一列设置为复选框。为此,我创建了一个 CheckBoxCellEditor,但我不知道为什么它不起作用。

名为 tableName 的列显示其值正常。

列规格如下。

    String[] COLUMN_HEADINGS = {"Select", "Table name"};

    TableColumnLayout tableLayout = new TableColumnLayout();
    parent.setLayout(tableLayout);

    //Set what would display each column
    TableViewerColumn selectCheckBox = createTableViewerColumn(COLUMN_HEADINGS[0], 0);
    selectCheckBox.getColumn().setResizable(false);
    selectCheckBox.setLabelProvider(new ColumnLabelProvider(){
         @Override
         public String getText(Object element) {
                 return null;
         }
    });

    //Set what would display each column
    TableViewerColumn tableName = createTableViewerColumn(COLUMN_HEADINGS[1], 1);
    tableName.getColumn().setResizable(false);
    tableName.setLabelProvider(new ColumnLabelProvider(){
        public String getText(Object element) {
            if(element instanceof TableMetaData && element != null)
                return ((TableMetaData)element).getName();
            return super.getText(element);
        }
    });

    //Set the dimensions of each column
    tableLayout.setColumnData(selectCheckBox.getColumn(), new ColumnWeightData(10));
    tableLayout.setColumnData(tableName.getColumn(), new ColumnWeightData(90));

    //Set column type (checkbox)
    selectCheckBox.setEditingSupport(new ResourcesConfigCheckEditingSupport(this));

EditingSupport 如下:

public class ResourcesConfigCheckEditingSupport extends EditingSupport{
    private CheckboxCellEditor cellEditor;


    public ResourcesConfigCheckEditingSupport(ColumnViewer viewer) {
        super(viewer);
        // TODO Auto-generated constructor stub
        cellEditor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
    }

    @Override
    protected CellEditor getCellEditor(Object element) {
        // TODO Auto-generated method stub
        return cellEditor;
    }

    @Override
    protected boolean canEdit(Object element) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected Object getValue(Object element) {
        // TODO Auto-generated method stub
        return ((TableMetaData) element).getIsSelected();
    }

    @Override
    protected void setValue(Object element, Object value) {
        // TODO Auto-generated method stub
        ((TableMetaData) element).setIsSelected(Boolean.valueOf((boolean) value));
        getViewer().update(element, null);

    }
}

TableMetaData 对象决定复选框是否被选中。如何修复我的代码以使其正常工作?

谢谢。

Vogella 有一个完整的例子,您可以在这里找到:http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html

在第 4 节中使用了 CheckboxCellEditor。可能对你有帮助。

为了让它发挥作用,我使用了 Vogella 的图像解决方案。问题是图像无法在 table 的单元格中居中,并且它们出现在它们的左侧。我曾尝试使用 "X" 和“-”,但我不知道第一列发生了什么,它既不使文本居中。因此,为了解决这个问题,我更改了列的顺序并使用了 unicode 字符。

编辑支持解决方案代码如下:

public class ResourcesConfigCheckEditingSupport extends EditingSupport{
    private CheckboxCellEditor cellEditor;


    public ResourcesConfigCheckEditingSupport(ColumnViewer viewer) {
        super(viewer);
        // TODO Auto-generated constructor stub
        //cellEditor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
        cellEditor = new CheckboxCellEditor(((TableViewer)viewer).getTable());
    }

    @Override
    protected CellEditor getCellEditor(Object element) {
        // TODO Auto-generated method stub
        return cellEditor;
    }

    @Override
    protected boolean canEdit(Object element) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected Object getValue(Object element) {
        // TODO Auto-generated method stub
        return ((TableMetaData) element).getIsSelected();
    }

    @Override
    protected void setValue(Object element, Object value) {
        // TODO Auto-generated method stub
        ((TableMetaData) element).setIsSelected(Boolean.valueOf((boolean) value));
        getViewer().update(element, null);

    }
}

以及创建列的以下内容:

public class ResourcesConfigViewer extends TableViewer{

    public ResourcesConfigViewer(Composite parent,int style) 
    {
        super(parent, style);

        //Get the table and set the headers visible and the lines
        Table table = getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        //Create the columns of the tableviewer
        createColumns(parent);

        //Set the tableviewer contentProvider
        setContentProvider(new ResourcesConfigTableContentProvider());
    }

    /*With this function we create the tableviewer columns and how they will (TextCell) and what they would display*/
    private void createColumns(Composite parent)
    {   
        //Headers of the table
        String[] COLUMN_HEADINGS = {"Table name", "Select"};

        TableColumnLayout tableLayout = new TableColumnLayout();
        parent.setLayout(tableLayout);

        //Set what would display each column
        TableViewerColumn tableName = createTableViewerColumn(COLUMN_HEADINGS[0], 0);
        tableName.getColumn().setResizable(false);
        tableName.setLabelProvider(new ColumnLabelProvider(){
            public String getText(Object element) {
                if(element instanceof TableMetaData && element != null)
                    return ((TableMetaData)element).getName();
                return super.getText(element);
            }
        });

        //Set what would display each column
        TableViewerColumn selectCheckBox = createTableViewerColumn(COLUMN_HEADINGS[1], 1);
        selectCheckBox.getColumn().setResizable(false);
        selectCheckBox.setLabelProvider(new ColumnLabelProvider(){
            public String getText(Object element) {
                if(element instanceof TableMetaData && element != null){
                    if(((TableMetaData)element).getIsSelected()){
                        return Character.toString((char)0x2611);
                    }else{
                        return Character.toString((char)0x2610);
                    }
                }
                return super.getText(element);
                //return null;
            }

            @Override
            public Image getImage(Object element) {
                /*if(element instanceof TableMetaData && element != null){
                    if(((TableMetaData)element).getIsSelected()){
                        return checked;
                    }else{
                        return unChecked;
                    }
                }
                return super.getImage(element);*/
                return null;
            }
        });



        //Set the dimensions of each column
        tableLayout.setColumnData(selectCheckBox.getColumn(), new ColumnWeightData(10));
        tableLayout.setColumnData(tableName.getColumn(), new ColumnWeightData(90));

        //Set column type (checkbox)
        selectCheckBox.setEditingSupport(new ResourcesConfigCheckEditingSupport(this));
    }

    //Creates the column
    private TableViewerColumn createTableViewerColumn(String header, int idx) 
    {
        //To put checkbox centered in cell.
        int infoLocation = SWT.LEFT;
        if(idx == 1){
            infoLocation = SWT.CENTER;
        }
        TableViewerColumn column = new TableViewerColumn(this, infoLocation, idx);
        column.getColumn().setText(header);
        column.getColumn().setResizable(true);
        column.getColumn().setMoveable(true);

        return column;
    }
}

Solution Image

TableViewer viewer = new TableViewer(container, SWT.CHECK | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);

但我必须手动更改输入

viewer.getTable().addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                TableItem item = (TableItem) event.item;
                Delay aDelay = (Delay) event.item.getData();
            }
        });