Create a GroupShape and get parent node

Hello.
I’m trying to create a GroupShape and add text box type Shapes to it. The only way to create a new GroupShape seems to be via a constructor:

GroupShape groupShape = new GroupShape(documentBuilder.getDocument());
Shape shape = new Shape(documentBuilder.getDocument(), ShapeType.TEXT_BOX)
groupShape.appendChild(shape);

However, a group shape initialised this way does not have a parent node. So when I attempt to reposition the cursor using DocumentBuilder documentBuilder.moveTo(groupShape.getParentNode()) this fails due to parent node being null.

If I’m not mistaken, a shape typically has a parent node when it is created using DocumentBuilder like this:
Shape shape = documentBuilder.insertShape(ShapeType.TEXT_BOX, 0d, 0d);

Is it possible to create a GroupShape with DocumentBuilder? If not, how can GroupShape parent be assigned, and which node should act as its parent?

@Martin123 groupShape.getParentNode returns you the node outside the groupShape. If you create a GroupShape and insert it with builder.insertNode(groupShape), you will have a parent node in front of the GroupShape. Builder is used to create and insert nodes more quickly. Running Shape shape = documentBuilder.insertShape(ShapeType.TEXT_BOX, 0d, 0d) will create a new node and insert it into the document, then the shape can have a parent node, which will be a paragraph.

Unfortunatly, there is no way to create group shape using document builder.

Thank you for the explanation! I hadn’t realized I needed to use builder.insertNode(groupShape). It’s all working correctly now.

1 Like