gpt4 book ai didi

java - 向表中添加按钮 (java swt)

转载 作者:搜寻专家 更新时间:2023-11-01 03:42:07 25 4
gpt4 key购买 nike

我正在尝试复制与此类似的 UI:

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

我一直在按照作者的说明(没有成功)关于如何在我的表格的每一列中创建按钮。我的项目和他的项目之间的区别在于我尝试使用 Tree 而不是 Table,并且我是在 eclipse TreeViewer< 的上下文中进行的 插件。从理论上讲,实现似乎应该很简单,但我似乎无法让它发挥作用。

这是我的代码,它很容易复制,因为它只是带有树查看器的示例 Java PDT 示例 View ,加上 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();
}

最佳答案

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

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

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

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

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

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

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

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

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​​ntalminimumWidth 值的行时,会显示正常的树单元格渲染器并且按钮不可见。

关于java - 向表中添加按钮 (java swt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com