很高兴今天成为了CSDN的专家,所以再忙也要抽空写一篇博客。最近公司有个需求要在RCP应用中用树状结构显示层级关系。我找了半天的开源框架,最后发现Eclipse REST最符合现在的需求。REST是专门用来显示图状效果的,废话少说了上效果,上代码。
public class DeviceTreeViewSWT { public static void main(String[] args) { Display d = new Display(); Shell shell = new Shell(d); Image image1 = Display.getDefault().getSystemImage(SWT.ICON_INFORMATION); Image image2 = Display.getDefault().getSystemImage(SWT.ICON_WARNING); Image image3 = Display.getDefault().getSystemImage(SWT.ICON_ERROR); shell.setLayout(new FillLayout()); shell.setSize(800, 800); Graph g = new Graph(shell, SWT.NONE); g.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println(((Graph) e.widget).getSelection()); } }); g.setConnectionStyle(ZestStyles.CONNECTIONS_DIRECTED); GraphNode n1 = new GraphNode(g, SWT.NONE, "Virtual"); n1.setBackgroundColor(new Color(d,255,0,0)); GraphNode n2 = new GraphNode(g, SWT.NONE, "Shanghai"); GraphNode n3 = new GraphNode(g, SWT.NONE, "Beijing"); GraphNode n4 = new GraphNode(g, SWT.NONE, "AGM001"); GraphNode n5 = new GraphNode(g, SWT.NONE, "AGM002"); GraphNode n6 = new GraphNode(g, SWT.NONE, "AGM003"); GraphNode n7 = new GraphNode(g, SWT.NONE, "AGM004"); GraphNode n8 = new GraphNode(g, SWT.NONE, "AGM005"); GraphNode n9 = new GraphNode(g, SWT.NONE, "AGM006"); GraphNode n10 = new GraphNode(g, SWT.NONE, "AGM007"); GraphNode n11 = new GraphNode(g, SWT.NONE, "AGM008"); // class PathFigure extends PolylineConnection { // public PathFigure() { // setTargetDecoration(new PolylineDecoration()); // setConnectionRouter(new ManhattanConnectionRouter()); // } // } new GraphConnection(g, SWT.NONE, n1, n2); new GraphConnection(g, SWT.NONE, n1, n3); new GraphConnection(g, SWT.NONE, n2, n4); new GraphConnection(g, SWT.NONE, n2, n5); new GraphConnection(g, SWT.NONE, n2, n6); new GraphConnection(g, SWT.NONE, n3, n7); new GraphConnection(g, SWT.NONE, n3, n8); new GraphConnection(g, SWT.NONE, n5, n9); new GraphConnection(g, SWT.NONE, n5, n10); new GraphConnection(g, SWT.NONE, n4, n11); //g.setLayoutAlgorithm(new TreeLayoutAlgorithm(LayoutStyles.ENFORCE_BOUNDS), true); //g.setLayoutAlgorithm(new HorizontalTreeLayoutAlgorithm(LayoutStyles.ENFORCE_BOUNDS),true); g.setLayoutAlgorithm(new RadialLayoutAlgorithm(LayoutStyles.ENFORCE_BOUNDS),true); //g.setLayoutAlgorithm(new GridLayoutAlgorithm(LayoutStyles.ENFORCE_BOUNDS), true); shell.open(); while (!shell.isDisposed()) { while (!d.readAndDispatch()) { d.sleep(); } } image1.dispose(); image2.dispose(); image3.dispose(); } }改下LayoutAlgorithm可以看到更炫的辐射状图

效果不错吧,用来显示图,树等都非常的方便
如果你用Jface封装的GraphViewer,那MVC使用起来更是方便.