无法从swt中的表中删除tableitem

时间:2018-10-05 11:30:14

标签: java arraylist hashmap set swt

当我从left_group_table(List)中选择List5时,应从middle_group_table(Contact)中删除所有属于List5的项目。如果列表中包含多个项目,则应删除联系表中的所有项目。请在下面找到该应用程序的屏幕截图和代码段。预先感谢!

enter image description here

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;
            }

        }

1 个答案:

答案 0 :(得分:0)

您的表应该有removeAll()方法,可以使用。

middle_group_table.removeAll();

编辑: 要删除单行,您需要获取要删除的元素的正确索引,而没有任何LINQ的最简单方法是:

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);
}