2

I made an ImageView array:

public static ImageView[] array = new ImageView[2];

Then set the ImageView:

ImageView img = new ImageView(new Image("sample.png"));
array [0] = img;
array [1] = img;

Then add it to the screen:

root.getChildren().add(array [0]);
root.getChildren().add(array [1]);

ERROR:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@78e30575

So, where is my mistake?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
tommY
  • 109
  • 4

1 Answers1

2

The error message really says it all. You can't add the same child twice, and in your snippet both array[0] and array[1] point to the same object. You could, however, add two objects that use the same image:

array[0] = new ImageView(new Image("sample.png"));
array[1] = new ImageView(new Image("sample.png"));
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    It would be more efficient to have 2 ImageViews, but only one Image. It is only the duplicate node (InageView) which is causing the error. If you have two different nodes, but point them both to the same Image, that won’t be a problem and is preferred. – jewelsea Sep 12 '21 at 08:20