将按钮添加到表(java swt) [英] Adding buttons to a table (java swt)

查看:159
本文介绍了将按钮添加到表(java swt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to replicate a UI similar to this:

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

,我一直在跟踪作者的指示(没有成功),了解如何创建我表的每一列中的按钮。我的项目之间的区别是我正在尝试使用一个 Tree 而不是一个,我在做它在eclipse TreeViewer 插件的上下文中。在理论上看来,实施应该是直截了当的,但我似乎无法让它工作。

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. In theory it seems the implementation should be straightforward, but I can't seem to get it to work.

这是我的代码,它很容易复制,因为它只是示例Java PDT sampleview与treeviewer,加上十几个额外的行code>的createPartControl 。您在这里看不到的所有内容与示例相同:

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. 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?

SWT的$ javadoc TreeEditor 类给出一个树编辑器的示例,指出

The javadoc of the SWT TreeEditor class gives an example of a tree editor stating that


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

"The editor must have the same size as the cell and must not be any smaller than 50 pixels."

以下确保在这个例子中满足这些条件:

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.

我使用了标准的RCP邮件示例项目并添加了您的按钮实验代码。在内部,我使用简单的文本按钮。

I used the standard RCP Mail Example project and added your "Button experimentation" code to it. 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);
  }
}

当我执行这样的代码时,按钮显示向上。当我注释出设置编辑器的 grabHorizo​​ntal minimumWidth 值的行时,会显示正常的树单元格渲染器,并显示按钮不可见。

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.

这篇关于将按钮添加到表(java swt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆