buy now
log in
MindFusion

Q: Can I use your Java diagramming control in an SWT (Standard Widget Toolkit) application?

A: JDiagram is a Swing component, and it can be embedded into SWT application using the SWT/AWT Bridge library. A minimal SWT program that creates and displays a scrollable DiagramView is shown below.


import java.awt.Frame;
import java.awt.Panel;

import javax.swing.JScrollPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.mindfusion.diagramming.DiagramView;

public class SwtApp
{ 
 public static void main(String[] args) 
 {
	 // create SWT main window
	 Display display = new Display ();
	 Shell shell = new Shell(display);
	 shell.setLayout(new FillLayout());
	 Composite composite = new Composite(shell, SWT.EMBEDDED | SWT.NO_BACKGROUND);

	 // add a Swing frame to the SWT composite control
	 Frame frame = SWT_AWT.new_Frame(composite);
	 Panel panel = new Panel(new java.awt.BorderLayout());
	 frame.add(panel);

	 // add a DiagramView to the Swing frame
	 DiagramView flowchart = new DiagramView();
	 JScrollPane scrollPane = new JScrollPane(flowchart);
	 panel.add(scrollPane);

	 // run the SWT event loop
	 shell.open();
	 while (!shell.isDisposed())
	 {
		 	if (!display.readAndDispatch())
		 		display.sleep();
	 }
	 display.dispose ();
 }
}

Note that Swing and SWT process events in different threads. Hence if you need to update the SWT user interface in response to a diagram event, you must run the SWT processing code using the Display.asyncExec or Display.syncExec methods. If you need to update the diagram in response to an SWT event, run the diagram processing code using SwingUtilities.invokeLater or SwingUtilities.invokeAndWait methods. See the Swing/SWT Integration topic at eclipse.org for further information on thread synchronization and additional SWT/AWT Bridge tips.

Copyright © 2001-2024 MindFusion LLC. All rights reserved.
Terms of Use - Contact Us