Put a String on the edge of JUNG graph

726 views Asked by At

I'm trying to put a String text to an edge of a JUNG tree graph. See bellow example. What I tried is to simply add this line:

graph.addEdge("arrow", "Vmain", "Vsecond");

... but without any results (see bellow screenshot). I need some advices please.

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.commons.collections15.Factory;
import edu.uci.ics.jung.algorithms.layout.TreeLayout;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Forest;
import edu.uci.ics.jung.graph.DelegateForest;
import edu.uci.ics.jung.graph.DelegateTree;
import edu.uci.ics.jung.graph.Tree;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;

public class TreeVisualiser extends JApplet {

    Forest<String, String> graph;

    Factory<DirectedGraph<String, String>> graphFactory = new Factory<DirectedGraph<String, String>>() {
        public DirectedGraph<String, String> create() {
            return new DirectedSparseMultigraph<String, String>();
        }
    };

    Factory<Tree<String, String>> treeFactory = new Factory<Tree<String, String>>() {
        public Tree<String, String> create() {
            return new DelegateTree<String, String>(graphFactory);
        }
    };

    VisualizationViewer<String, String> vv;
    String root;
    TreeLayout<String, String> treeLayout;

    @SuppressWarnings({"rawtypes", "unchecked"})
    public TreeVisualiser() {

        // create a simple graph for the demo
        graph = new DelegateForest<String, String>();

        createTree();

        treeLayout = new TreeLayout<String, String>(graph);
        vv = new VisualizationViewer<String, String>(treeLayout, new Dimension(600, 600));
        vv.setBackground(Color.white);
        vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        // add a listener for ToolTips
        vv.setVertexToolTipTransformer(new ToStringLabeller());

        Container content = getContentPane();
        final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
        content.add(panel);

        final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();

        vv.setGraphMouse(graphMouse);

        JComboBox modeBox = graphMouse.getModeComboBox();
        modeBox.addItemListener(graphMouse.getModeListener());
        graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

        final ScalingControl scaler = new CrossoverScalingControl();

        JButton plus = new JButton("+");
        plus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                scaler.scale(vv, 1.1f, vv.getCenter());
            }
        });
        JButton minus = new JButton("-");
        minus.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                scaler.scale(vv, 1 / 1.1f, vv.getCenter());
            }
        });

        JPanel scaleGrid = new JPanel(new GridLayout(1, 0));
        scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));

        JPanel controls = new JPanel();
        scaleGrid.add(plus);
        scaleGrid.add(minus);
        controls.add(scaleGrid);
        controls.add(modeBox);

        content.add(controls, BorderLayout.SOUTH);
    }

    private void createTree() {

        graph.addEdge("arrow", "Vmain", "Vsecond");
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Container content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        content.add(new TreeVisualiser());
        frame.pack();
        frame.setVisible(true);
    }
}
1

There are 1 answers

0
Apopei Andrei Ionut On

Oh, I missed this line of code. Now it is working as I want.

vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());