Adding buttons to a table (java swt)

[亡魂溺海] 提交于 2019-12-12 12:13:17

问题


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

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

回答1:


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?

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

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

[Edit: What I did to reproduce the behaviour]

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

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.



来源:https://stackoverflow.com/questions/11801589/adding-buttons-to-a-table-java-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!