Search code examples
javaeclipseeclipse-pluginjfacercp

how to use setAllChecked(boolean) method of CheckBoxTableViewer class of jFace?


protected Control createDialogArea( Composite parent )
{
    Composite composite = new Composite( parent, SWT.NONE );
    composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
    composite.setLayout( new GridLayout( 2, false ) );
    Group consumedCmp = new Group( composite, SWT.BORDER );
    consumedCmp.setLayout( new GridLayout( 1, false ) );
    consumedCmp.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true, 1, 1 ) );
    consumedCmp.setText( reg.getString("ASSIGN_AS_TXT") );
    btnMsConsumed = new Button( consumedCmp, SWT.RADIO );
    btnMsConsumed.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, false, false, 1, 1 ) );
    btnMsConsumed.setText( ME_CONSUMED );
    btnRequired = new Button( consumedCmp, SWT.RADIO );
    btnRequired.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, false, false, 1, 1 ) );
    btnRequired.setText( REQUIRED );
    Group tableCmp = new Group( composite, SWT.BORDER );
    tableCmp.setLayout( new GridLayout( 1, false ) );
    tableCmp.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true, 1, 1 ) );
    tableCmp.setText( PART_ASSIGNMENT_INDICATOR );
    tableViewer = CheckboxTableViewer.newCheckList( tableCmp, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL );
    table = tableViewer.getTable();
    table.setLinesVisible( true );
    table.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true, 1, 1 ) );
    Composite buttonCmp = new Composite( tableCmp, SWT.NONE );
    buttonCmp.setLayout( new GridLayout( 2, false ) );
    buttonCmp.setLayoutData( new GridData( SWT.FILL, SWT.FILL, false, false, 1, 1 ) );
    btnSelectAll = new Button( buttonCmp, SWT.NONE );
    btnSelectAll.setLayoutData( new GridData( SWT.RIGHT, SWT.CENTER, true, false, 1, 1 ) );
    btnSelectAll.setText( reg.getString("SELECT_ALL_TXT") );
    btnSelectAll.addListener( SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent( Event event )
        {
            //tableViewer.setAllChecked( true );
            for(TableItem item : table.getItems())
                item.setChecked(true);
        }
    });
    Button btnDeselectAll = new Button( buttonCmp, SWT.NONE );
    btnDeselectAll.setLayoutData( new GridData( SWT.LEFT, SWT.CENTER, true, false, 1, 1 ) );
    btnDeselectAll.setText( reg.getString("DESELECT_ALL_TXT") );
    btnDeselectAll.addListener( SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent( Event event )
        {
            //tableViewer.setAllChecked( false );
            for(TableItem item : table.getItems())
                item.setChecked(false);
        }
    });
    initTable();
    return composite;
}

private void initTable()
{
    Iterator iter = ind.iterator();
    while( iter.hasNext() )
    {
        TableItem item = new TableItem( table, SWT.NONE );
        // this code works
        item.setText( ( String ) iter.next() );
        item.setChecked(true);
    }
    // the below code doesnot works
    //tableViewer.setAllChecked( true );
}

Can anyone give examples of the above case? At the start, all the items of the table should be checked.

I have some 5 items in table. By default, all the items should be checked. I will have 2 buttons select all , deselect all which should select all the items and deselect all the items respectively.

Can anyone give example for this?


Solution

  • If you use a JFace CheckboxTableViewer (or any other JFace viewer) you must use the JFace content provider together with a label provider and the setInput call.

    You must not create TableItems like you are doing - the JFace code won't know about the items and your code will misbehave. When you use JFace viewers you never manipulate the underlying table or tree items.

    You appear to have a list ind which could be used as the input for the viewer so you would do something like:

    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    
    tableViewer.setLabelProvider(new LabelProvider());
    
    tableViewer.setInput(ind);
    

    (and remove all your code creating TableItems)

    ArrayContentProvider can deal with input which is an array or which implements the List interface.

    With the table set up correctly using the providers the setAllChecked method will work.