Prefuse学习(三)从数据库中读取数据并SWT显示

之前用的是swing来显示Prefuse.后来想用SWT显示这样更和window的界面匹配.

参考网址

http://cping1982.blog.51cto.com/601635/130275/

 

所以把学习(二)中的,main方法修改为

/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DatabaseDataSource datasrc = null; org.eclipse.swt.widgets.Display display1 = new org.eclipse.swt.widgets.Display(); Shell shell = new Shell(); shell.setImage(SWTResourceManager.getImage(TestMySql.class, "/icon/user_business_boss.png")); try { //get database connection datasrc = ConnectionFactory.getDatabaseConnection( driverName, dbURL, userName, userPwd); //create a table of data Table nodes = datasrc.getData("select * from node"); Table edges = datasrc.getData("select * from edge"); Graph graph = new Graph(nodes, edges, false, "id", "sid", "tid"); Visualization vis = new Visualization(); vis.add("graph", graph); LabelRenderer r = new LabelRenderer("name"); r.setRoundedCorner(8, 8); vis.setRendererFactory(new DefaultRendererFactory(r)); int[] palette = new int[] { ColorLib.rgb(255,180,180), ColorLib.rgb(190,190,255) }; DataColorAction fill = new DataColorAction("graph.nodes", "gender", Constants.NOMINAL, VisualItem.FILLCOLOR, palette); ColorAction textColor = new ColorAction("graph.nodes", VisualItem.TEXTCOLOR, ColorLib.gray(0)); ColorAction edgesColor = new ColorAction("graph.edges", VisualItem.STROKECOLOR, ColorLib.gray(200)); ActionList color = new ActionList(); color.add(fill); color.add(textColor); color.add(edgesColor); ActionList layout = new ActionList(Activity.INFINITY); layout.add(new ForceDirectedLayout("graph")); layout.add(new RepaintAction()); vis.putAction("color", color); vis.putAction("layout", layout); Display d = new Display(vis); d.setSize(720, 500); d.addControlListener(new DragControl()); d.addControlListener(new PanControl()); d.addControlListener(new ZoomControl()); d.addControlListener(new NeighborHighlightControl()); // JFrame frame = new JFrame("Prefuse"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // frame.setSize(800, 600); frame.add(d); // frame.setVisible(true); int width = 800; int height = 600; //获得容器 Container container = frame.getContentPane(); vis.run("color"); vis.run("layout"); shell.setSize(new Point(width, height)); shell.setLayout(new FillLayout()); shell.setText("Prefuse in SWT"); // 新建swt组件 Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED); // 注入组件,并返回为frame Frame frm = SWT_AWT.new_Frame(composite); // 加载容器 frm.add(container); // 打开shell shell.open(); } catch (Exception e) { e.printStackTrace(); } while (!shell.isDisposed()) { if (!display1.readAndDispatch()) display1.sleep(); } }

 

运行效果如图所示:

你可能感兴趣的:(prefuse,SWT/RCP/XWT)