问题
Updated: question is incorrect because of low knowledge of the subject. Sorry.
I'm trying to create a small application that shows a graph that contains nodes
and connections
between them. Both nodes and connections are complex, I mean they can have another components in it, like labels.
I have a big Pane which plays a canvas role. I'm going to add and remove elements from it. The problem is that I want to add
or remove
graph elements dynamically, using buttons or context menu. Sort of Paint for Graphs
:) And I have no idea how to implement it.
Especially I desperately need help in dynamical adding/removing mechanism
. I would be very grateful for your help!
回答1:
Just get the child list of the pane you want to add stuff to, and add the stuff when the appropriate action occurs.
FlowPane pane = new FlowPane();
Button addNode = new Button("Add");
addNode.setOnAction(e -> pane.getChildren().add(new Circle(10));
Notes:
If you want to use a Pane rather than a FlowPane, then Pane has no internal layout, so you need to also set the layoutX and layoutY properties appropriately when you add to the Pane.
If you want to change the rendering order of nodes in the Pane (e.g. which nodes are rendered on the bottom and which on top), then you do that by adding new nodes at an appropriate position in the child list; for example,
pane.getChildren().add(0, new Circle(10))
, will add a circle that is rendered underneath all other children of the pane rather than on top.
来源:https://stackoverflow.com/questions/35489213/add-components-dynamically-in-javafx