Search code examples
javajung

JUNG change Layout dynamically


I created a graph using JUNG, and I want to add a combobox that give the user the possibility to change the used layout (Circle, KK, FR, etc.)

But I couldn't do that.

that's how I visualize my graph:

// The Layout<V, E> is parameterized by the vertex and edge types
        this.layout = new CircleLayout<Ressource,Float>(this.graph);

        layout.setSize(new Dimension(500, 500)); // sets the initial size of the
                                                    // layout space
        // The BasicVisualizationServer<V,E> is parameterized by the vertex and
        // edge types

        this.vv = new BasicVisualizationServer<Ressource, Float>(layout);
        vv.setPreferredSize(new Dimension(550, 550)); // Sets the viewing area
                                                        // size

        // Adjust the edges thikness
        Transformer<Float, Stroke> edgeStroke = new Transformer<Float, Stroke>() {
            @Override
            public Stroke transform(Float arg0) {
                return new BasicStroke(arg0);
            }
        };

        vv.getRenderContext().setEdgeStrokeTransformer(edgeStroke);

        // Show vertex and edge labels
        vv.getRenderContext().setVertexLabelTransformer(
                new Transformer<Ressource, String>() {
                    public String transform(Ressource r) {
                        return (r.nom);
                    }
                });

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

I tried to create a whole new BasicVisualizationServer object having each time a different Layout, but it didn't work, It sticks with the first layout (Circle in my case).

What is the best way to change layout ?

Thanks guys !


Solution

  • The source file for this demo (in your distribution) demonstrates how to do it: http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/samples/ShowLayouts.html