简体   繁体   English

向表中添加按钮(java swt)

[英]Adding buttons to a table (java swt)

I'm trying to replicate a UI similar to this: 我正在尝试复制类似于此的UI:

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

and I have been following the authors instructions (without success) on how to create buttons that are in each column of my table. 我一直在关注如何创建表格每列中的按钮的作者说明(没有成功)。 The difference between my project as his is I am trying to use a Tree rather than a Table , and I am doing it in the context of an eclipse TreeViewer plugin. 我的项目与他的区别在于我正在尝试使用Tree而不是Table ,而我正在使用eclipse TreeViewer插件进行上下文。 In theory it seems the implementation should be straightforward, but I can't seem to get it to work. 从理论上讲,实现似乎应该是直截了当的,但我似乎无法让它发挥作用。

Here is my code, it is easily replicate-able as it is just the sample Java PDT sampleview with a treeviewer, plus a dozen or so extra lines in the createPartControl . 这是我的代码,它很容易复制,因为它只是带有treeviewer的示例Java PDT sampleview,以及createPartControl的十几个额外行。 Everything you don't see here is just the same as the sample: 你在这里看不到的一切都和样品一样:

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

When you say that you can't seem to get it to work, do you mean that you can't see the buttons in your Tree ? 当你说你似乎无法让它工作时,你的意思是你看不到Tree的按钮吗?

The javadoc of the SWT TreeEditor class gives an example of a tree editor stating that SWT TreeEditor类的javadoc给出了一个树编辑器的例子,说明了这一点

"The editor must have the same size as the cell and must not be any smaller than 50 pixels." “编辑器必须与单元格具有相同的大小,并且不得小于50像素。”

The following lines assure that these conditions are met in the example: 以下行确保在示例中满足这些条件:

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

So if you add these lines to your editor the buttons should be visible. 因此,如果将这些行添加到编辑器中,则按钮应该是可见的。

[Edit: What I did to reproduce the behaviour] [编辑:我做了什么来重现行为]

I used the standard RCP Mail Example project and added your "Button experimentation" code to it. 我使用了标准的RCP Mail Example项目,并将“Button实验”代码添加到其中。 Inside, I used simple text buttons. 在里面,我使用简单的文本按钮。

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

When I execute the code like this, the buttons show up. 当我执行这样的代码时,按钮会显示出来。 When I comment out the lines setting the editor's grabHorizontal and minimumWidth values, the normal tree cell renderer is shown and the buttons are not visible. 当我注释掉设置编辑器的grabHorizontalminimumWidth值的grabHorizontal ,会显示普通的树单元格渲染器,并且按钮不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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