TableViewerColumn中的JFace按钮

时间:2012-08-24 05:23:43

标签: button jface tableviewer

TableViewerColumn中是否可以有一个按钮?有几个帖子证实了这一点,但我发现没有实际可行的代码。我已经读过一个关于DialogCellEditor的内容,那是什么内容?

此致 马库斯

1 个答案:

答案 0 :(得分:1)

由于这似乎是一个常见问题,我尝试了一种解决方法。我使用图像作为标签并添加编辑支持,如下所示:

        col = createTableViewerColumn(titles[10], bounds[10], 10);
    col.setEditingSupport(new DeleteSupport(viewer));
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public Image getImage(Object element) {
            return new Image(ApplicationRunner.getApp().getShell()
                    .getDisplay(), "ressources/images/delete.png");
        }

        @Override
        public String getText(Object element) {
            return "";
        }
    });

在DeleteSupport类(扩展EditingSupport)中,您必须让canEdit()返回false,因此图像不可选。但是,你不能使用getValue()。所以,在返回false之前,我在canEdit()中做了我所做的一切。这与简单的按钮相同。

DeleteSupport如下所示:

public class DeleteSupport extends EditingSupport {

private final TableViewer viewer;

public DeleteSupport(TableViewer viewer) {
    super(viewer);
    this.viewer = viewer;
}

@Override
protected CellEditor getCellEditor(Object element) {
    return new TextCellEditor(viewer.getTable());
}

@Override
protected boolean canEdit(Object element) {

            // if confirmed, try to delete the customer
    if (MessageDialog.openConfirm(  ApplicationRunner.getApp().getShell(),
                    "Confirm delete",
                    "Soll " + ((Customer) element).getFirstname()
                    + " " + ((Customer) element).getLastname()
                    + " be deleted? Cannot be undone!")) {

        try {
            CustomerDAO.getInstance().delete(((Customer) element).getId());
        } catch (SQLException e) {
            // TODO something
        }
    }

            // reload anyways
    try {
        viewer.setInput(CustomerDAO.getInstance().getAll());
    } catch (SQLException e) {
        // TODO something else
    }
    viewer.refresh();

    return false;
}

@Override
protected Object getValue(Object element) {
    return "";
}

@Override
protected void setValue(Object element, Object value) {
}

}