0

I can currently add different labels based on their class type to a gridpane while iterating though an array. How would I be able to do this, but with certain images instead? I.e if the object in the array is of type: Class 1, then add the image banana.png

Here's the code I am currently using to add labels:

for (int x = 0; x < Array.length; x++) {
        for (int y = 0; y < Array[x].length; y++) {
            Label labelName;
            if (Array[x][y] instanceof Class1) {
                labelName = new Label("Class1");
            else {
                labelName = new Label(Class2");
            }

            theGrid.add(labelName, x, y);
  • Don't use an `Array` of `Object`. – SedJ601 Apr 05 '18 at 19:14
  • Shouldn't be too hard `Image img = (Array[x][y] instanceof Class1) ? image1 : image2; theGrid.add(new ImageView(img), x, y);`. It would probably be better to avoid the use of `instanceof` in this case though and change the implementation of the classes contained in the array to allow you to handle those things without considering the type of the object in the array... – fabian Apr 05 '18 at 19:14

1 Answers1

0

You can replace label with ImageView

Image img = new Image("\banana.png");

for (int x = 0; x < Array.length; x++) {
        for (int y = 0; y < Array[x].length; y++) {
            ImageView imageView;
            if (Array[x][y] instanceof Class1) {
                imageView = new ImageView(img);
            else {
                //do something
            }

            theGrid.add(imageView, x, y);