1
我有一個帶有內容和標籤提供程序的TreeViewer。當我設置輸入時,數據不顯示,我只有一個空白的視圖。我究竟做錯了什麼?TreeViewer不顯示輸入
在下面的代碼中,我展示瞭如何創建TreeViwer和提供者。父母應該是字符串,孩子是父母的子字符串(即字母)。
我如何創建TreeViwer:
Tree tree = new Tree(top, SWT.CHECK | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
tree.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
TreeViewer treeViewer = new TreeViewer(tree, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
treeViewer.setContentProvider(new TestContentProvider());
treeViewer.setLabelProvider(new TestLabelProvider());
List<String> input = new ArrayList<String>();
input.add("abc");
input.add("test");
treeViewer.setInput(input);
的供應商:
public class TestLabelProvider extends LabelProvider {
@Override
public String getText(Object element) {
if (element instanceof String) {
return ((String) element);
}
return "none";
}
}
public class TestContentProvider implements ITreeContentProvider {
private static final Object[] EMPTY_ARRAY = new Object[0];
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof List) {
return ((List<String>) inputElement).toArray();
} else {
return EMPTY_ARRAY;
}
}
@Override
public boolean hasChildren(Object element) {
if (element instanceof String) {
return true;
}
return false;
}
@Override
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof String) {
String s = (String) parentElement;
return s.split("");
}
return EMPTY_ARRAY;
}
@Override
public void dispose() {
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
@Override
public Object getParent(Object element) {
return null;
}
}