Skip to content
Advertisement

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. To do that I have created a CheckBoxCellEditor, but I don´t know why it isn´t working.

The column called tableName displays it´s values 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:

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. How can I fix my code in order to make it work?

Thank you.

Advertisement

Answer

To make it work I used Vogella´s solution using images. 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. So to fix that problem I have changed column´s order and I have used unicode characters.

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

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement