Search code examples
eclipse-pluginswteclipse-rcpjface

Clearing the contents of SWT-CheckboxTableViewer


I have implemented as shown below.

enter image description here

The issue what I am facing is in clearing the contents of CheckboxTableViewer, Right now I am keeping track of the items present in the CheckboxTableViiewer and then removing the 0th element, but this is causing problems when the user moves around the list very swiftly.

list= new org.eclipse.swt.widgets.List(container, SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
list.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));

Code for Check Box

table = new Table(container, SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
table.setHeaderVisible(true);
checkboxTableViewer = new CheckboxTableViewer(table);

in the listener, I tried to get the table and clear the table but that was not working.

checkboxTableViewer .getTable().clearAll();

please let me know how to proceed.

Updated with Code

package com.checkagain.importWizards;


public class PageTwo extends WizardPage {
    private Text text1;
    private Composite container;

    org.eclipse.swt.widgets.List single;
    Table table ;
    CheckboxTableViewer checkboxTableViewer;
    List elementList= new ArrayList();



    public PageTwo() {
        super("PageTwo");
        setTitle("PageTwo");
        setDescription("Fake Wizard: PageTwo");
        elementList.add("1"); 
        elementList.add("2");
        elementList.add("3");

        elementList.add("4");
        elementList.add("5");
        elementList.add("6");

    }

    @Override
    public void createControl(Composite parent) {
        container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        container.setLayout(new FillLayout());
        layout.numColumns = 2;


        single= new org.eclipse.swt.widgets.List(container, SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
        single.add("one");
        single.add("two");

        single.add("three");
        single.add("four");

        single.add("five");
        single.add("six");
        //single.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
        //////Code for Check Box


         table = new Table(container, SWT.CHECK | SWT.FULL_SELECTION | SWT.VIRTUAL);
         //table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
        table.setHeaderVisible(true);
        checkboxTableViewer = new CheckboxTableViewer(table);

        single.addSelectionListener(new SelectionListener() {

            @Override
            public void widgetSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

                //additional code added on suggestion
                checkboxTableViewer.setInput(new String[0]);
                checkboxTableViewer.refresh();              

                checkboxTableViewer.add(elementList.get(single.getSelectionIndex()));
            }

            @Override
            public void widgetDefaultSelected(SelectionEvent arg0) {
                // TODO Auto-generated method stub

            }
        });


        setControl(container);
        setPageComplete(true);
    }

    public String getText1() {
        return text1.getText();
    }
}

Solution

  • When you are using the JFace table viewers you should avoid accessing the underlying Table - the table viewer manages that.

    Also you are specifying the SWT.VIRTUAL flag - tables behave very differently with this flag it is only useful for large tables.

    You can clear the viewer by setting the table viewer input to an empty array using setInput or by making your content provider return an empty elements array and calling refresh - a simple example:

    CheckboxTableViewer checkboxTableViewer = CheckboxTableViewer.newCheckList(shell, SWT.FULL_SELECTION | SWT.BORDER);
    
    checkboxTableViewer.setContentProvider(ArrayContentProvider.getInstance());
    checkboxTableViewer.setLabelProvider(new LabelProvider());
    
    final List<String> input = new ArrayList<>();
    input.add("entry 1");
    input.add("entry 2");
    input.add("entry 3");
    
    checkboxTableViewer.setInput(input);
    
    Button clear = new Button(shell, SWT.PUSH);
    clear.setText("Clear");
    
    clear.addListener(SWT.Selection, event ->
      {
        input.clear();
    
        checkboxTableViewer.refresh();
      });