Asperger
Asperger

Reputation: 3222

JavaFX add node (Programm) after or before another node (XML)

If I append a node like this:

HBox child = new HBox();
HBox fooBar = (HBox) doc.lookup("#fooBar");

fooBar.getChildren().add(child);

It might work but not the way I want it to because I want to define the position. What if I wanted the child before or after fooBar?

<HBox>
  <HBox id="first"></HBox>
  <HBox id="fooBar"></HBox>
  <HBox id="last"></HBox>
</HBox>

Upvotes: 0

Views: 1285

Answers (1)

Abaddon666
Abaddon666

Reputation: 1623

fooBar.getParent().getChildren() returns a ObservableList which, as it is inheriting from java.util.List has a method add(int index, E element)(further information in spec)

Adding your new Node at the right position may do the trick. The following code adds the child before fooBar.

int fooBarIndex = fooBar.getParent().getChildren().indexOf(fooBar);
fooBar.getParent().getChildren().add(fooBarIndex, child)

Upvotes: 4

Related Questions