先看一下效果图:
代码结构是这样的:
好的,现在看一下代码:
package model; import java.util.List; public interface ITree { public String getName(); public void setName(String name); public List getChildren(); public void setChildren(List children); }
首先定义接口。再实现两个类。
package model; import java.util.ArrayList; import java.util.List; public class Country implements ITree{ private String name; private List children=new ArrayList(); public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Member> getChildren() { return children; } public void setChildren(List children) { this.children = children; } }
package model; import java.util.List; public class Member implements ITree{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public List getChildren() { // TODO Auto-generated method stub return null; } @Override public void setChildren(List children) { // TODO Auto-generated method stub } }
以List的形式组织树形结构:
package model; import java.util.ArrayList; import java.util.List; public class Factory { public static List createTree(){ Country mouseCountry=new Country(); mouseCountry.setName("鼠球王国"); Country dogCountry=new Country(); dogCountry.setName("狗狗王国"); Member mouseBuble=new Member(); mouseBuble.setName("小鼠球"); Member mouseGray=new Member(); mouseGray.setName("小灰鼠"); ArrayList list=new ArrayList(); list.add(mouseBuble); list.add(mouseGray); mouseCountry.setChildren(list); Member dogBu=new Member(); dogBu.setName("布布狗"); Member dogBrick=new Member(); dogBrick.setName("专狗"); list=new ArrayList(); list.add(dogBrick); list.add(dogBu); dogCountry.setChildren(list); ArrayList countryList=new ArrayList(); countryList.add(mouseCountry); countryList.add(dogCountry); return countryList; } }
实现他的两个Provider:
package com.test; import java.util.List; import model.ITree; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.Viewer; public class TreeContentProvider implements IStructuredContentProvider,ITreeContentProvider{ @Override public void dispose() { // TODO Auto-generated method stub } @Override public void inputChanged(Viewer arg0, Object arg1, Object arg2) { // TODO Auto-generated method stub } @Override public Object[] getChildren(Object arg0) { // TODO Auto-generated method stub ITree tree=(ITree)arg0; List list=tree.getChildren(); if(list!=null){ return list.toArray(); } return new Object[0]; } @Override public Object getParent(Object arg0) { // TODO Auto-generated method stub return null; } @Override public boolean hasChildren(Object element) { // TODO Auto-generated method stub ITree tree=(ITree)element; List list=tree.getChildren(); return !(list==null || list.isEmpty()); } @Override public Object[] getElements(Object arg0) { // TODO Auto-generated method stub if(arg0 instanceof List){ List list=(List)arg0; return list.toArray(); } return new Object[0]; } }
package com.test; import model.ITree; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.graphics.Image; public class TreeLabelProvider extends LabelProvider implements ILabelProvider{ public String getText(Object element){ ITree node=(ITree)element; return node.getName(); } public Image getImage(Object element){ return null; } }
最后是总的,将他们联系在一起:
package com.test; import model.Factory; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class LBMShow { public static void main(String[] args) { final Display display=Display.getDefault(); final Shell shell=new Shell(); shell.setSize(500,500); shell.setText("TreeTable"); final TreeViewer treeViewer=new TreeViewer(shell, SWT.BORDER | SWT.SCROLL_PAGE); treeViewer.setContentProvider(new TreeContentProvider()); treeViewer.setLabelProvider(new TreeLabelProvider()); treeViewer.setInput(Factory.createTree()); //这里可以将List的结构以树形结构显示出来。 shell.pack(); shell.open(); shell.setLayout(new FillLayout()); //没这句话竟然出不来效果! shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } }
//******************************************************************************************************************************
需要注意的是,要添加如下的包: