8

是否可以将删除按钮添加到表格中的单元格?

我有一个有 5 列的表,我想添加第 6 列。我希望第 6 列的每一行都有一个删除按钮。

示例行:

| 10002 | 部分 | 金属 | 001 | 是 | Remove |

这样,用户只需单击按钮即可删除任何不需要的行。

我在表中有一个标记列,它是ComboBox. 我创建了一个扩展类EditingSupport

我是否需要创建另一个类扩展EditingSupport,但创建一个按钮而不是ComboBox

编辑

public class AplotDataTableViewer extends TableViewer
{

    public AplotDataTableViewer(Composite parent, int style) 
    {
        super(parent, style);

        Table table = getTable();
        GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        table.setLayoutData(gridData);
        createColumns();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        setContentProvider(new ArrayContentProvider());
    }

    private void createColumns() {

      String[] titles = { "ItemId", "RevId", "PRL", "Dataset Name", "EC Markup" };
      int[] bounds = { 150, 150, 100, 150, 100 };

      TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
      col.setLabelProvider(new ColumnLabelProvider() {
         public String getText(Object element) {
            if(element instanceof AplotDataModel.AplotDatasetData)
               return ((AplotDataModel.AplotDatasetData)element).getDataset().toString();
            return super.getText(element); 
            }
         }); 

      col = createTableViewerColumn(titles[1], bounds[1], 1);
      col.setLabelProvider(new ColumnLabelProvider() {
         public String getText(Object element) {
            if(element instanceof AplotDataModel.AplotDatasetData)
               return ((AplotDataModel.AplotDatasetData)element).getRev().toString();
            return super.getText(element); 
            }
         });

      col = createTableViewerColumn(titles[2], bounds[2], 2);
      col.setLabelProvider(new ColumnLabelProvider() {
         public String getText(Object element) {
            if(element instanceof AplotDataModel.AplotDatasetData)
               return ((AplotDataModel.AplotDatasetData)element).getPRLValue().toString();
            return super.getText(element); 
            }
         });
      col = createTableViewerColumn(titles[3], bounds[3], 3);
      col.setLabelProvider(new ColumnLabelProvider() {
         public String getText(Object element) {
            if(element instanceof AplotDataModel.AplotDatasetData)
               return ((AplotDataModel.AplotDatasetData)element).getDatasetName().toString();
            return super.getText(element); 
            }
         });

       col = createTableViewerColumn(titles[4], bounds[4], 4);
       col.setLabelProvider(new ColumnLabelProvider() {
          public String getText(Object element) {
             if(element instanceof AplotDataModel.AplotDatasetData)
                return ((AplotDataModel.AplotDatasetData)element).getMarkupValue();
             return super.getText(element); 
             }
          });

       col.setEditingSupport(new OptionEditingSupport(this));

    }

    private TableViewerColumn createTableViewerColumn(String header, int width, int idx) 
    {
        TableViewerColumn column = new TableViewerColumn(this, SWT.LEFT, idx);
        column.getColumn().setText(header);
        column.getColumn().setWidth(width);
        column.getColumn().setResizable(true);
        column.getColumn().setMoveable(true);

        return column;
    }
}

编辑

col = createTableViewerColumn(titles[5], bounds[5], 5);
   col.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public void update(ViewerCell cell) {
         TableItem item = new TableItem(getTable(),SWT.NONE);
         Button button = new Button(getTable(),SWT.NONE);
         button.setText("X");
         getControl().setBackground(item.getBackground());
         TableEditor editor = new TableEditor(getTable());
         editor.grabHorizontal  = true;
         editor.grabVertical = true;
         editor.setEditor(button , item, columnIndex);
         editor.layout();
         }
      });
4

4 回答 4

23

这是示例工作版本。

public class TableEditorTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        shell.setLayout(new FillLayout());


        TableViewer viewer = new TableViewer(shell);
        viewer.getTable().setHeaderVisible(true);
        viewer.getTable().setLinesVisible(true);
        viewer.setContentProvider(new ArrayContentProvider());

        TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("First Name");
        column.setWidth(100);
        TableViewerColumn firstNameCol = new TableViewerColumn(viewer, column);
        firstNameCol.setLabelProvider(new ColumnLabelProvider(){

            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getFirstName();
            }

        });

        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Last Name");
        column.setWidth(100);
        TableViewerColumn lastNameCol = new TableViewerColumn(viewer, column);
        lastNameCol.setLabelProvider(new ColumnLabelProvider(){

            @Override
            public String getText(Object element) {
                Person p = (Person)element;

                return p.getLastName();
            }

        });




        column = new TableColumn(viewer.getTable(), SWT.NONE);
        column.setText("Actions");
        column.setWidth(100);
        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();
            }

        });



        Person p1 = new Person();
        p1.setFirstName("George");
        p1.setLastName("Burne");

        Person p2 = new Person();
        p2.setFirstName("Adam");
        p2.setLastName("Silva");

        Person p3 = new Person();
        p3.setFirstName("Nathan");
        p3.setLastName("Cowl");

        List<Person> persons = new ArrayList<Person>();
        persons.add(p1);
        persons.add(p2);
        persons.add(p3);

        viewer.setInput(persons);

        shell.open();
        while(!shell.isDisposed())
        {

            if(!display.readAndDispatch())
            {
                display.sleep();
            }
        }

        display.dispose();

    }


    private static class Person
    {

        String firstName;
        String lastName;

        Person()
        {

        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

    }


}
于 2012-09-19T00:23:04.720 回答
3

除了@sambi.reddy 的回答之外,还有一条评论说 - “确保在查看器输入更改时处理这些按钮”。这就是我必须做的才能让那部分工作。

我使用的框架有一个 IStructuredContentProvider 的实现,所以在重写的 inputChanged(...) 中,我必须输入以下代码:-

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    // This will dispose of all the control button that were created previously
    if (((TableViewer)viewer).getTable() != null && ((TableViewer)viewer).getTable().getChildren() != null) {
        for (Control item : ((TableViewer)viewer).getTable().getChildren()) {
            // at this point there are no other controls embedded in the viewer, however different instances may require more checking of the controls here.
            if ((item != null) && (!item.isDisposed())) {  
                item.dispose();
            }
        }
    }
}

这工作正常,除非您有一个刷新表(即 viewer.setInput(List))调用作为按钮操作的结果。当我添加这个时,以下行有时会返回仍然存在但正在处理的按钮:

if(buttons.containsKey(cell.getElement()))

所以我需要将这一行更新为:

if (buttons.containsKey(cell.getElement()) && !buttons.get(cell.getElement()).isDisposed())

如果仍然需要它们,这会导致重新创建任何处理按钮。

于 2014-07-15T06:00:28.023 回答
1

所选答案的一件重要事情是,您不应该在每次更新方法调用时创建 TableEditor 的实例,这会降低性能甚至使应用程序无法响应。

@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();
            }
        }
于 2014-04-23T10:26:24.410 回答
0

下面的代码应该适合你

TableItem item = (TableItem) item;
Button button = new Button(table,swt.none);
button.setText("Remove");
control.setBackground(item.getBackground());
TableEditor editor = new TableEditor(table);
editor.grabHorizontal  = true;
editor.grabVertical = true;
editor.setEditor(button , item, columnIndex);
editor.layout();
于 2012-09-18T16:10:00.550 回答