转自点击打开链接
一是在树加上几个列对象TreeColumn,二是创建一个新的用于表格型树的标签器。
TreeViewer tv = new TreeViewer(shell, SWT.BORDER); Tree tree = tv.getTree(); tree.setHeaderVisible(true); TreeColumn column = new TreeColumn(tree, SWT.LEFT); column.setText("名称"); column.setWidth(150); column = new TreeColumn(tree, SWT.LEFT); column.setText("子结点数"); column.setWidth(80); column = new TreeColumn(tree, SWT.LEFT); column.setText("性别"); column.setWidth(40); tv.setContentProvider(new TreeViewerContentProvider()); tv.setLabelProvider(new MyTableLableProvider());标签器:
public class MyTableLableProvider implements ITableLabelProvider { public String getColumnText(Object element, int columnIndex) { ITreeEntry entry = (ITreeEntry) element; switch (columnIndex) { //根据列号,来决定显示记录的哪个字段值 case 0: return entry.getName(); case 1: List list = entry.getChildren(); if (list == null) return ""; return String.valueOf(list.size()); case 2: if (entry instanceof PeopleEntity) { boolean sex = ((PeopleEntity) entry).isSex(); return sex ? "男" : "女"; } return ""; default: return ""; } } //-----------------------以下方法暂时不用,空实现--------------------------- public Image getColumnImage(Object element, int columnIndex) {return null;} public void addListener(ILabelProviderListener listener) {} public void removeListener(ILabelProviderListener listener) {} public void dispose() {} public boolean isLabelProperty(Object element, String property) {return false;} }
还有个ContainerCheckedTreeViewer,是带复选框的树:
可以不同设置tv.setLabelProvider,直接每一列都 TreeViewerColumn.setLabelProvider(new VariableNameCellLabelProvider());
private void createTreeColumn(final ContainerCheckedTreeViewer tv) {
final String[] headers = new String[] { "VariableName", "CustomName" };
tv.getTree().setHeaderVisible(true);
final TreeViewerColumn columVariableName = new TreeViewerColumn(tv, SWT.NONE);
columVariableName.getColumn().setText(headers[0]);
columVariableName.getColumn().setWidth(140);
columVariableName.getColumn().setResizable(true);
columVariableName.getColumn().setMoveable(true);
columVariableName.setLabelProvider(new VariableNameCellLabelProvider());
final TreeViewerColumn columCustomName = new TreeViewerColumn(tv, SWT.NONE);
columCustomName.getColumn().setText(headers[1]);
columCustomName.getColumn().setWidth(100);
columCustomName.getColumn().setResizable(true);
columCustomName.getColumn().setMoveable(true);
columCustomName.setLabelProvider(new CustomNameCellLabelProvider());
columCustomName.setEditingSupport(new CustomNameEditingSupport(tv));
}
private class VariableNameCellLabelProvider extends CellLabelProvider {
@Override
public void update(final ViewerCell cell) {
final Object element = cell.getElement();
if (element instanceof Entry) {
final Entry<?, ?> variable = (Entry<?, ?>) element;
cell.setText((String) variable.getValue());
}
}
}
// TODO
private class CustomNameCellLabelProvider extends CellLabelProvider {
@Override
public void update(final ViewerCell cell) {
final Object element = cell.getElement();
if (element instanceof Entry) {
final Entry<?, ?> variable = (Entry<?, ?>) element;
cell.setText("Custom");
}
}
}
private class CustomNameEditingSupport extends EditingSupport {
public CustomNameEditingSupport(final ColumnViewer viewer) {
super(viewer);
}
@Override
protected CellEditor getCellEditor(final Object element) {
return new TextCellEditor((Composite) getViewer().getControl(), SWT.NONE);
}
@Override
protected boolean canEdit(final Object element) {
return true;
}
@Override
protected Object getValue(final Object element) {
return "正在编辑";
}
@Override
protected void setValue(final Object element, final Object value) {
}
}