简体   繁体   English

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

[英]How can I create a checkbox in a TableViewer of JFace?

I have created a tableViewer with two columns and I want to make one of them a checkbox.我创建了一个包含两列的 tableViewer,我想将其中一个设置为复选框。 To do that I have created a CheckBoxCellEditor, but I don´t know why it isn´t working.为此,我创建了一个 CheckBoxCellEditor,但我不知道为什么它不起作用。

The column called tableName displays it´s values OK.名为 tableName 的列显示它的值 OK。

The column specification is the following.列规格如下。

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

And the EditingSupport is the following: 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);

    }
}

The TableMetaData object decides if the Checkbox is going to be selected or not. TableMetaData 对象决定是否选择复选框。 How can I fix my code in order to make it work?如何修复我的代码以使其正常工作?

Thank you.谢谢你。

Vogella has a complete example which you will find here: http://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html Vogella 有一个完整的例子,你可以在这里找到: http ://www.vogella.com/tutorials/EclipseJFaceTableAdvanced/article.html

In section 4 a CheckboxCellEditor is used.在第 4 节中使用了CheckboxCellEditor It might help you.它可能会帮助你。

To make it work I used Vogella´s solution using images.为了使它工作,我使用了 Vogella 的图像解决方案。 The problem is that the images can´t be centered in the table´s cell and they appear in the left of them.问题是图像不能在表格的单元格中居中,它们出现在它们的左侧。 I have tried to use "X" and "-", but I don´t know what happens with the first column that it neither centers the text.我尝试使用“X”和“-”,但我不知道第一列不会使文本居中。 So to fix that problem I have changed column´s order and I have used unicode characters.所以为了解决这个问题,我改变了列的顺序,并使用了 unicode 字符。

The solution code is the following for editing support:编辑支持的解决方案代码如下:

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

    }
}

And the following to create the columns:以及以下用于创建列的内容:

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

but i must change input manually但我必须手动更改输入

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM