问题
When adding nodes to my DelegateTree
, they don't appear visually in the order I added them.
I've been looking for a solution, but haven't found anything yet.
Is there anyone who might know how to change this?
Thanks in advance!
EDIT: My code
/**
* Generate a visualization of the decision tree.
*
* @param tree
* The decision tree
* @return A component to be placed inside a JPanel
*/
public static GraphZoomScrollPane generateTree(Tree tree,
GraphicalUserInterface gui) {
/* Create a new tree */
DelegateTree<Node, Edge> graphTree = new DelegateTree<Node, Edge>();
/* Add all nodes and vertices to the tree */
graphTree.addVertex(tree.getRoot());
addChildren(tree.getRoot(), graphTree);
/* Create the visualization */
DynamicTreeLayout<Node, Edge> treeLayout = new DynamicTreeLayout<Node, Edge>(graphTree, 100, 100);
VisualizationViewer<Node, Edge> vv = new VisualizationViewer<Node, Edge>(
treeLayout);
vv.setDoubleBuffered(true);
vv.setBackground(Color.WHITE);
vv.getRenderContext().setEdgeLabelTransformer(
new ToStringLabeller<Edge>());
vv.getRenderContext().setEdgeShapeTransformer(
new EdgeShape.Line<Node, Edge>());
vv.getRenderContext().setVertexLabelTransformer(
new ToStringLabeller<Node>());
vv.getRenderer().getVertexLabelRenderer()
.setPosition(Renderer.VertexLabel.Position.S);
vv.addGraphMouseListener(new ClickNode(gui, vv));
final DefaultModalGraphMouse<Node, Edge> graphMouse = new DefaultModalGraphMouse<Node, Edge>();
graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);
vv.setGraphMouse(graphMouse);
final GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);
return gzsp;
}
/**
* Recursively add all nodes and edges to the tree.
*
* @param node
* The parent node
* @param tree
* The tree visualization
*/
private static void addChildren(Node node, DelegateTree<Node, Edge> tree) {
for (int i = 0; i < node.getChildren().size(); i++) {
tree.addChild(new Edge(node.getChildren().get(i).getEdgeLabel()), node, node
.getChildren().get(i));
addChildren(node.getChildren().get(i), tree);
}
}
回答1:
Use an underlying implementation for the DelegateTree that either sorts its vertices according to their natural ordering (*Sorted*Graph) or that uses insertion ordering (*Ordered*Graph).
来源:https://stackoverflow.com/questions/10215718/jung-placing-tree-nodes-in-order