Search code examples
javaswttableviewer

how to delete selected rows (multiple rows) from CheckboxTableViewer when button clicks? (table is connected to oracle database)


i hava a CheckboxTableViewer which has 10 columns, and the table is filled from database, and i have a button outside the table named as "Delete", what i want to do is:- when i select rows using check box (multiple selection also) and when i press the "delete" button , i want the selected rows should get deleted from the database, and the tableviewer shuold get refreshed.

am pasting my tableviewer code below:-

    final CheckboxTableViewer dataTable = CheckboxTableViewer.newCheckList(TableComposite2, SWT.MULTI | SWT.H_SCROLL   
            | SWT.V_SCROLL | SWT.BORDER |SWT.DM_FILL_BACKGROUND|SWT.FULL_SELECTION);
    dataTable .getTable().setHeaderVisible(true);
    dataTable .getTable().setLinesVisible(true);
    dataTable .setContentProvider(new ArrayContentProvider());


//Action Check box
    TableColumn columnCHead=new TableColumn(dataTable .getTable(),SWT.NONE);
    columnCHead.setText("Delete");
    columnCHead.setWidth(50);
    // setting column input
    TableViewerColumn columnC=new TableViewerColumn(dataTable ,columnCHead);
    columnC.setLabelProvider(new ColumnLabelProvider()
    {
        public String getText(Object Element)
        {

            return null;
        }
    });

    TableColumn columnFS1Head=new TableColumn(dataTable .getTable(),SWT.NONE);
    columnFS1Head.setText("SOURCE DIRECTORY");
    columnFS1Head.setWidth(300);

    TableViewerColumn columnFS1=new TableViewerColumn(dataTable ,columnFS1Head);
    columnFS1.setLabelProvider(new ColumnLabelProvider()
    {
        public String getText(Object Element)
        {
            AgedFileMaster a=(AgedFileMaster)Element;
            return a.getDIRECTORY_PATH();
        }

enter code here});

...... and i have a button for delete operation,(outside the table), when i press delete button, i want the selected rows to get deleted... am beginner to SWT. anyone please help......


Solution

  • Use addSelectionListener on your Button control to be notified when the button is pressed:

    button.addSelectionListener(new SelectionAdapter()
      {
        public void widgetSelected(SelectionEvent event)
        {
          // TODO handle delete here
        }
     });
    

    You need to do two things to remove the data - first update your data model to remove the objects and secondly tell the table viewer that the model has changed.

    You can do something like this:

    dataTable.getTable().setRedraw(false);  // Stop redraw during update
    
    IStructuredSelection selection = (IStructuredSelection)dataTable.getSelection();
    
    for (Iterator<?> iterator = selection.iterator(); iterator.hasNext(); )
     {
       Object selectedObject = iterator.next();
    
       // TODO remove from data model array
    
       // Tell table view the object has been removed
    
       dataTable.remove(selectedObject);
     }
    
    dataTable.getTable().setRedraw(true);  // Allow updates to be drawn
    

    An alternative to calling dataTable.remove on each object is to call dataTable.refresh once at the end. There is also a variant of remove which accepts an array of objects.