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);
}
});
}