Skip to main content

Add node at runtime

1 reply [Last post]
Joined: 2007-09-08

I want to add a node at run time but I have an eception: only a BranchGroup node may be added

My code:
public MyBehavior(TransformGroup TG) {
targetGroup = TG;
public void processStimulus(Enumeration criteria) {
targetGroup.addChild(new ColorCube(0.1));


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-05-04


Instead of passing a TransformGroup as a parameter for your behavior, pass the root of your scenegraph.

Be sure you set the capability BranchGroup.ALLOW_CHILDREN_EXTEND so you can add children at runtime.

The exception you get results from the implementation of BranchGroup. You can only add BranchGroups to superior live or compiled BranchGroups!

So if you want to have an object as a child of a TransformGroup, the way to include it would be this:

TransformGroup cubeTG = new TransformGroup();
//set some capabilities or do some transformations here if needed
cubeTG.addChild(new ColorCube(0.1));

BranchGroup cubeBG = new BranchGroup();
//set some capabilities here if needed


Of course you can arrange the content much more elegantly. This is the easiest way of adding objects during runtime.

- Thomas