Once you have added a JDiagram reference to your project, you can access the public classes defined in JDiagram packages. The most important classes are Diagram, which represents the diagram data, and DiagramView, which is a Swing component that displays the diagram. You usually create and initialize a new instances of the Diagram and DiagramView classes in the same place where you create and initialize the rest of the user interface of your application. Associate the diagram to be displayed by this view by calling the setDiagram method. To let users scroll the diagram, add the DiagramView object to a JScrollPane container. Thus, a minimal application containing a scrollable diagram that can be used to draw flowcharts looks like this:
Java
Copy Code
|
---|
package flowcharter; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JScrollPane; import com.mindfusion.diagramming.*; public class MainWindow extends JFrame { public MainWindow() { diagram = new Diagram(); diagramView = new DiagramView(); diagramView.setDiagram(diagram); JScrollPane scrollPane = new JScrollPane(diagramView); getContentPane().add(scrollPane); Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); setSize(size); } public static void main(String[] args) { MainWindow window = new MainWindow(); window.setVisible(true); } private DiagramView diagramView; private Diagram diagram; }
|
The initialization of the JDiagram control can be very different depending on what you will use it for, but there are certain properties you will most likely need to adjust in all cases. Here they are:
- Choose a Behavior for the control, which defines how JDiagram must interpret actions done with the mouse by users of your application. The default value, LinkShapes specifies that users can draw diagram nodes and connect them with diagram links. Another value, LinkTables allows drawing tables and relationships between them. The Pan behavior allows panning the view by dragging the mouse. Modify bans users from drawing new items, but allows them to modify existing ones. Call the setBehavior method of the DiagramView class to adjust the control's behavior.
- There are other methods of the DiagramView class for customizing the behavior of the diagram and its items - take a look at getCustomBehavior and setCustomBehavior, getCustomLinkType and setCustomLinkType, getCustomNodeType and setCustomNodeType.
- There are various methods that let you customize the general appearance of your diagram, such as setBackBrush, setBackgroundImage, setBoundsPen, setGridColor and so on.
- There is a set of methods that let you specify default attributes for diagram items. The names of these methods usually correspond to the name of the attribute they provide a default value for, prefixed by the name of the appropriate item class. For example, the method that lets you specify the text of the link's label is called DiagramLink.setText, while the default value for new links can be set through Diagram.setLinkText.
Once you have initialized JDiagram, you usually use the DiagramListener interface to handle various events, such as linkCreated and linkModified. These events let you take specific actions when diagram items are created or modified, such as updating the internal state of your application.
Browse the other sections of this help file to learn more about the functionality provided by the MindFusion.Diagramming classes, methods and properties. Follow the tutorials to learn how to populate the diagram model programmatically or create custom item types.