When adding a node to a force directed layout, how can I get the node to appear at specific (X,Y) coordinates rather than the top-left corner of the visualization window?

Whenever I add a free-floating node (not connected to any other nodes by edges) to my visualization, it always appears in the top-left corner and just stays there until the user clicks and drags it to where they want. How can I get the node to appear in the center of the screen when added or where the user last clicked?

1

There are 1 best solutions below

0
On

I found the following works: You get the corresponding visual item of the node and use the setEndX and setEndY.

val node = graph.addNode()
val vi = visualization.getVisualItem(GROUP_GRAPH, node)
vi.setEndX(desiredXPos)
vi.setEndY(desiredYPos)