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

StackOverflow https://stackoverflow.com/questions/22371348

  •  14-06-2023
  •  | 
  •  

Question

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......

Était-ce utile?

La 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.

Autres conseils

TableViewerColumn actionsNameCol = new TableViewerColumn(viewer, column);
    actionsNameCol.setLabelProvider(new ColumnLabelProvider(){
        //make sure you dispose these buttons when viewer input changes
        Map<Object, Button> buttons = new HashMap<Object, Button>();


        @Override
        public void update(ViewerCell cell) {

            TableItem item = (TableItem) cell.getItem();
            Button button;
            if(buttons.containsKey(cell.getElement()))
            {
                button = buttons.get(cell.getElement());
            }
            else
            {
                 button = new Button((Composite) cell.getViewerRow().getControl(),SWT.NONE);
                button.setText("Remove");
                buttons.put(cell.getElement(), button);
            }
            TableEditor editor = new TableEditor(item.getParent());
            editor.grabHorizontal  = true;
            editor.grabVertical = true;
            editor.setEditor(button , item, cell.getColumnIndex());
            editor.layout();
        }

    });

Delete selected rows (multiple rows) from Table when button clicks (Database Connectivity)

//Java ArrayList class uses a dynamic array for storing the elements

List<String> id_list=new ArrayList<String>();

//Button Text:Selected Row Delete   

Button btnNewButton = new Button(parent, SWT.NONE);
    btnNewButton.setText("Selected Row Delete");
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            TableItem[] item=table.getItems();
            for(int i=0;i<table.getItemCount();i++)
            {
                if(item[i].getChecked()&&!item[i].getText(1).equals(""))
                {
                    String id=item[i].getText(1);
                    id_list.add(id);//Add ID into List 
                }
            }
            for(int j=0;j<id_list.size();j++)
            {
                //class:Test
                //Method:DeleteData(String ID) pass id to delete rows
                // type cast object into string

                Test.DeleteData((String) id_list.get(j));
            }
        }
    });
    btnNewButton.setImage(ResourceManager.getPluginImage("RCP_Demo", "icons/delete.png"));
    btnNewButton.setBounds(18, 370, 68, 23);

Test.java


public class Test {
    static Connection conn = null;
    static PreparedStatement presta=null;
    public static void DeleteData(String ID)
    {
        String url = "jdbc:sqlite:Demo.db";
        try{
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection(url);
            presta = conn.prepareStatement("delete from Student where sid=?");
            presta.setString(1, ID);
            presta.executeUpdate();
            DisplayData();
        }catch(Exception e)
        {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }//End DeleteData()
}//End Test class
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top