How can I minify my code in here, I am giving each 23 total imageview a eventhandler.
code works, but I like to make the code shorter. I tried to use for loops to shorten the code.
here is a example what I tried to do:

    for (int i = 0 ; i < imgArrayList.size() ; i++) {
        imgArrayList.get(i).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                selectedTile = i;
            }
        });
    }

but it can't because, the "i" inside the handle method is final. I can't pass the "i" inside the handle method.

my goal is, that a specific imageview, that has for example ".get(3)", that needs to print out 3.
so if the imageview have a .get(i), it needs to print out the exact same number of i.
btw, the imageviews are stored in a arraylist, if you are curious where the ".get" comes from.

    protected static void addImageViewMouseEvents() {
        UI_SelectionMenu.imgArrayList.get(0).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 0;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(1).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 1;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(2).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 2;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(3).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 3;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(4).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 4;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(5).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 5;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(6).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 6;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(7).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 7;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(8).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 8;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(9).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 9;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(10).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 10;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(11).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 11;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(12).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 12;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(13).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 13;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(14).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 14;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(15).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 15;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(16).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 16;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(17).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 17;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(18).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 18;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(19).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 19;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(20).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 20;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(21).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 21;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
        UI_SelectionMenu.imgArrayList.get(22).setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                UI_SelectionMenu.selectedTile = 22;
                System.out.println(UI_SelectionMenu.selectedTile);
            }
        });
    }

What you posted is not fully tested, reusable code. What you posted is a question which clearly falls under the default category of "discussion/question".

When you created this thread, right above the area where you pasted your code was:

DO NOT post code with errors or known bugs. Code snippets must be fully tested, reusable code to share with others.

Clearly you found those instructions confusing. Would you care to suggest alternate instructions?

You could use an ordinary inner class instead of the anonymous event handlers, and pass i to the constructor. Something like

class MyHandler extends Eventhandler {
  int i;
  MyHandler(int i) { this.i = i;}

   @Override
   public void handle(MouseEvent event) {
        UI_SelectionMenu.selectedTile = i;
         System.out.println(UI_SelectionMenu.selectedTile);
   }
 }

 ...

 for (int i = 0 ; i < imgArrayList.size() ; i++) {
        imgArrayList.get(i).setOnMouseClicked(new MyHandler(i));
}
commented: james, thank you very much. btw the eventhandler is a interface, so I had to implement instead of extends. But it still helped me a lot (: +1

eventhandler is a interface, so I had to implement instead of extends

That's right. Sorry about that. I'm still on Swing, so my JavaFX is a bit patchy. I just typed that code on my iPad so had no chance to run it through te compiler.
Still, glad it helped :))
JC

ps: Please mark this "solved" if you are happy with the solution for the benefit of future readers

Hre's another approach that IMHO is much cleaner... use the id property that's inherited by every JavaFX node.

When creating the controls set their id property to the index i
In the event handler get the node from the MouseEvent and get its id
Now you can use a single event handler, and all the info is kept in the JavaFX control where it belons.

commented: thanks man, I forgot the setid and getid thing. this makes my project much easier to work with. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.