向表中添加按钮(java swt)

时间:2012-08-03 18:49:00

标签: java eclipse eclipse-plugin swt

我正在尝试复制类似于此的用户界面:

http://librixxxi.blogspot.com/2011/06/punch-clock-021-and-clickable-edit.html

我一直在按照作者的说明(没有成功)说明如何创建表格每列中的按钮。我的项目与他的区别在于我正在尝试使用Tree而不是Table,而我正在使用eclipse TreeViewer插件进行上传。从理论上讲,实现似乎应该是直截了当的,但我似乎无法让它发挥作用。

这是我的代码,它很容易复制,因为它只是带有treeviewer的示例Java PDT sampleview,加上createPartControl中的十几个额外行。你在这里看不到的一切与样本相同:

 class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {

        public String getColumnText(Object obj, int i) {
            if(i == 0){
                return obj.toString();
            }
            return "";
        }
        public Image getColumnImage(Object obj, int i) {
            if(i == 0){
            String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
            if (obj instanceof TreeParent)
               imageKey = ISharedImages.IMG_OBJ_FOLDER;
            return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
            }
            return null;
        }
    }
    class NameSorter extends ViewerSorter {
    }

    /**
     * The constructor.
     */
    public ButtonView() {
    }

    /**
     * This is a callback that will allow us
     * to create the viewer and initialize it.
     */
    public void createPartControl(Composite parent) {
        viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

          Tree tree = viewer.getTree();
            tree.setLinesVisible(true);
            tree.setHeaderVisible(true);
            TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
            column1.setText("Name");
            column1.setWidth(400);
            TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
            column2.setText("Some info");
            column2.setWidth(300);


        // Button experimentation    
        TreeItem[] items = tree.getItems();
        for(int i = 0; i < items.length; i++){
            TreeEditor editor = new TreeEditor(tree);
            TreeItem item = items[i];

            Button button = new Button(tree, SWT.PUSH);

            button.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT));
            button.setSize(16,16);
            button.pack();

            editor.horizontalAlignment = SWT.RIGHT;
            editor.setEditor(button, item);
        }


        drillDownAdapter = new DrillDownAdapter(viewer);
        viewer.setContentProvider(new ViewContentProvider());
        viewer.setLabelProvider(new ViewLabelProvider());
        viewer.setSorter(new NameSorter());
        viewer.setInput(getViewSite());

        // Create the help context id for the viewer's control
        PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "ButtonTest.viewer");
        makeActions();
        hookContextMenu();
        hookDoubleClickAction();
        contributeToActionBars();
    }

1 个答案:

答案 0 :(得分:1)

当你说你似乎无法让它工作时,你的意思是你无法看到Tree中的按钮吗?

SWT TreeEditor类的javadoc给出了树编辑器的一个示例,说明

  

“编辑器必须与单元格大小相同,且不得小于50像素。”

以下几行确保在示例中满足这些条件:

editor.grabHorizontal = true;
editor.minimumWidth = 50;

因此,如果您将这些行添加到编辑器中,则按钮应该是可见的。

[编辑:我为重现行为做了什么]

我使用了标准的RCP邮件示例项目,并将“Button实验”代码添加到其中。在里面,我使用了简单的文本按钮。

public void createPartControl(Composite parent) {
  viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
  viewer.setContentProvider(new ViewContentProvider());
  viewer.setLabelProvider(new ViewLabelProvider());
  viewer.setInput(createDummyModel());

  experiment();
}

private void experiment() {

  // Button experimentation  
  Tree tree = viewer.getTree();
  TreeItem[] items = tree.getItems();

  for(int i = 0; i < items.length; i++){
    TreeEditor editor = new TreeEditor(tree);

    TreeItem item = items[i];

    Button button = new Button(tree, SWT.PUSH);

    button.setText("A");
    button.setSize(16,16);
    button.pack();

    editor.horizontalAlignment = SWT.RIGHT;
    editor.grabHorizontal = true;
    editor.minimumWidth = 50;
    editor.setEditor(button, item);
  }
}

当我执行这样的代码时,按钮会显示出来。当我注释掉设置编辑器的grabHorizontalminimumWidth值的行时,会显示普通的树单元格渲染器,并且按钮不可见。