Articles Projects Tips Downloads Contacts About

JConnector project.

On java forums I have seen multiple questions like this “How to implement connecting lines between components?” and decided to implement a simple example of such feature. The implementation is the same JComponent which can be added to a container. The JConnector has references to a pair of components. During painting it calculates connecting components bounds and defines two points for custom connecting line for start and end. Depending on connecting components layout and line properties it creates custom line and draw it.

As it was stated above the JConnector can be added directly to container. But such unnecessary components often screw layout so the connectors may be used in kind of canvas where user have to directly call their paint() methods.

This way was used in the demo app.

import javax.swing.*;
import java.awt.*;

/**
 * The collateral class contains array of connectors and renders them.
 * The rendering can be called in a different way. E.g. JConnectors cn be just
 * added as usual component. In this case programmer must care about their size,
 * and layout.
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * @author Stanislav Lapitsky
 * @version 1.0
 */
public class ConnectorContainer extends JPanel {
    JConnector[] connectors;
    public ConnectorContainer() {
    }

    public ConnectorContainer(JConnector[] connectors) {
        this.connectors = connectors;
    }

    public void setConnectors(JConnector[] connectors) {
        this.connectors = connectors;
    }

    public JConnector[] getConnectors() {
        return connectors;
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (connectors != null) {
            for (int i = 0; i < connectors.length; i++) {
                if (connectors[i] != null) {
                    connectors[i].paint(g);
                }
            }
        }
    }
}

The connecting lines may have pointer arrows. There are 4 types of line arrow:

  • LINE_ARROW_NONE – connecting line doesn’t have arrows.
  • LINE_ARROW_SOURCE – arrow points to source component.
  • LINE_ARROW_DEST – arrow points to destination component.
  • LINE_ARROW_BOTH – two arrows.

User may choose any of them to express connected components relations.

Another option is line color. End user specifies color of connecting line by a param passed in a constructor or set later by appropriate setter method.

JConnector provides a bunch of constructors with different parameters list to allow programmer choose the best one.

    /**
     * Constructs default connector.
     * @param source JComponent
     * @param dest JComponent
     */
    public JConnector(JComponent source, JComponent dest) 

    /**
     * Constructs a connector with specified arrow and color.
     * @param source JComponent
     * @param dest JComponent
     * @param lineArrow int
     * @param lineColor Color
     */
    public JConnector(JComponent source, JComponent dest, int lineArrow, Color lineColor) 

    /**
     * Constructs a connector with specified arrow, line type and color.
     * @param source JComponent
     * @param dest JComponent
     * @param lineArrow int
     * @param lineType int
     * @param lineColor Color
     */
    public JConnector(JComponent source, JComponent dest, int lineArrow, int lineType, Color lineColor) 

The demo application provides a simple GUI to configure connecting lines and their parameters. Just move connected components to see how the line will be changed.

The following image illustrates how the JConnector can be used. (Demo application results).

The source code is included in the .jar file (See src directory).

Click Downloads to get JConnector application.

Mirror stanislavl.blogspot.com to discuss or post a comment.