I have an arraylist made of 10 Comboboxes. I would like to load each combobox with n imageviews.
I get the data i need from a class which gaves me the correct data. The arraylist of arraylist you find in the code is the "simulation" of different dices one over another, creating different columns. For example i have max 10 columns each made by n dices.
Here is the code:
//LOADING DICES INTO MY COMBOBOXES
//List used to load strings
ObservableList<String> options = FXCollections.observableArrayList();
//Arraylist made of arraylist containing the data I need
ArrayList<ArrayList<Dice>> roundTrackData = gameManager.roundTrack.getDices();
System.out.println(roundTrackData);
for(int h=0; h<roundTrackData.size();h++){
System.out.println(" h Value:" + h);
ArrayList<Dice> testing = roundTrackData.get(h);
System.out.println(" Testing:" + testing);
System.out.println(" Testing size:" + testing.size());
for(int u=0; u<testing.size();u++){
System.out.println(" Inside cicle ");
String color = Character.toString(testing.get(u).getColor());
String value = testing.get(u).getValue().toString();
String diceRound = value+color+".png";
options.add(diceRound);
//listaComboBox is an array list containing 10 comboboxes
listaComboBox.get(h).setItems(options);
listaComboBox.get(h).setCellFactory(c -> new StatusListCell());
System.out.println("Dice color "+ color);
System.out.println("Dice value"+ value);
}
}
StatusListCell class:
public class StatusListCell extends ListCell<String> {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
System.out.println("IT'S NULL");
setGraphic(null);
setText(null);
if (item != null) {
System.out.println("IT'S NOT NULL!");
ImageView imageView = new ImageView(new Image(item));
imageView.setFitWidth(40);
imageView.setFitHeight(40);
setGraphic(imageView);
setText("a");
}
}
}
I have developed my code following this question: JavaFX ComboBox Image
The code works and insert the images i need into my Comboboxes, the problem is that it adds everytime more images to every combobox.
For Example: the first combobox loads properly the first 5 dices. When i add other 5 dices (which should only be added to the second combobox), they are added both to the first and second comboboxes, and i'll get two identical groups of elements (this keeps going on until the end).
I tried to change my code adding a options.clear()
before the second cycle, so the options ObservableList is resetted and i can add my elements from 0, and then add them into my h
combobox.
The problem is that i actually get no inserted imageView.
I also tried to move
listaComboBox.get(h).setItems(options);
listaComboBox.get(h).setCellFactory(c -> new StatusListCell());
out of the second for
cycle, but i still get nothing.
Any ideas of what's the problem? I have been trying for so long but i can't still figure out what's the real problem.