简体   繁体   中英

Unable to remove tableitem from table in swt

When i select List5 from left_group_table(List), all the items that belong to List5 should be removed from the middle_group_table(Contact). If the list contains mulitple items, all the items in the contact table should be removed. Please find the screenshot of the application and code snippet below. Thanks in advance!

在此处输入图片说明

public static ArrayList<String> allEmailsFortheSelectedList = new ArrayList<String>();
HashMap<Integer, ArrayList<String>> allEmailsForALLSelectedList;

tableCursor.addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent arg0) {
            final int selectionIndex = left_group_table.getSelectionIndex();

            if(left_group_table.getItem(selectionIndex).getChecked()) {
                int tempCount = 0;
                left_group_table.getItem(selectionIndex).setChecked(false);
                TableItem[] items = middle_group_table.getItems();

                if(allEmailsForALLSelectedList.containsKey(selectionIndex)) {
                    allEmailsForALLSelectedList.remove(selectionIndex);
                }

                Set<Entry<Integer, ArrayList<String>>>  set = allEmailsForALLSelectedList.entrySet();
                Iterator<Entry<Integer, ArrayList<String>>> itr = set.iterator(); 
                while(itr.hasNext()) 
                { 
                    HashMap.Entry<Integer, ArrayList<String>> entry = itr.next(); 
                    for(int i=0; i< entry.getValue().size(); i++) {
                        new TableItem(middle_group_table, SWT.NONE);
                        items[tempCount].setText(1, entry.getValue().get(i));
                        tempCount++;
                    }
                }

                tempCount = items.length;
                middle_group_table.setRedraw(true);

            }else {
                int middleGroupTableItemCount = 0;
                left_group_table.getItem(selectionIndex).setChecked(true);
                sendEmailslistName = left_group_table.getItem(selectionIndex).getText(1);

                int listId = SelectionDb.getUserContactListId(sendEmailslistName);

                allEmailsForALLSelectedList.put(selectionIndex, SelectionDb.getAllContactEmail(listId));

                for (int i = 0; i < SelectionDb.getAllContactEmail(listId).size(); i++) {
                    new TableItem(middle_group_table, SWT.NONE);
                }

                middle_group_table.setRedraw(true);

                TableItem[] items = middle_group_table.getItems();
                Set<Entry<Integer, ArrayList<String>>>  set = allEmailsForALLSelectedList.entrySet();
                Iterator<Entry<Integer, ArrayList<String>>> itr = set.iterator(); 
                while(itr.hasNext()) 
                { 
                    HashMap.Entry<Integer, ArrayList<String>> entry = itr.next(); 
                    for(int i=0; i< entry.getValue().size(); i++) {
                        items[middleGroupTableItemCount].setText(1, entry.getValue().get(i));
                        middleGroupTableItemCount++;
                    }
                }
                middleGroupTableItemCount = items.length;
            }

        }

There should be removeAll() method for your table, that you can use.

middle_group_table.removeAll();

EDIT: To remove a single row you need to get the right index of the element you want to remove, the simplest method without any LINQis this:

First get the right list you need:
List<String> itemsToRemove = ...getting the list5, from your code I don't understand how the list is holding.

Then you can just iterate in reverse way and remove.
for (int i = middle_group_table.getItemCount() - 1; i <= 0; i--)
{
    if (itemsToRemove.contains(items[i].getText()))
        middle_group_table.remove(i);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM