5
votes

I tried create a Java JGraphT visualization with Weight, for example:

http://i.stack.imgur.com/KXRrc.png

Now I want to change the edge label (ex: (a:b) to its edge weight (ex: 2). I tried to search in Javadoc but didn't see anything. Please help me thank you!

2

2 Answers

4
votes

Those edge labels came from the toString method of the edge object. Therefore you may create your own edge class and override the toString method in such a way that it yields the edge weight.

You may create your custom edge like this:

class MyWeightedEdge extends DefaultWeightedEdge {
  @Override
  public String toString() {
    return Double.toString(getWeight());
  }
}

And then create your graph like this which will make the graph create edges as instances of MyDefaultEdge and therefore display just edge weight in the visualization:

SimpleWeightedGraph<String,MyWeightedEdge> graph
  = new SimpleWeightedGraph<String,MyWeightedEdge>(MyWeightedEdge.class);
0
votes

JGraphT is implemented Generic so when you pass Edge class just implement toString() method in your respective class. E.g. suppose "City" is my Vertex class and "Path" is my Edge so implement toString() method in that

class Pathe{
  @Override
  public String toString() {
    return "Distant Between" +this.source +"this.dest" "is" + this.distance;
  }
}