我有一个由10个组合框组成的数组列表。我想用n个图像视图加载每个组合框。
我从一个给我提供正确数据的类中获得我需要的数据。您在代码中找到的arraylist的arraylist是不同骰子之间的“模拟”,创建不同的列。例如,我有最多10列,每列由n个骰子组成。
代码如下:
//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类:
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");
}
}
}
我根据这个问题开发了我的代码:JavaFX ComboBox Image
代码可以工作并将我需要的图像插入到我的组合框中,问题是它每次都会向每个组合框中添加更多的图像。
以为例,:第一个组合框正确加载前5个骰子。当我添加其他5个骰子(应该只添加到第二个组合框中)时,它们同时被添加到第一个和第二个组合框中,我将得到两组相同的元素(这将一直持续到最后)。
我试图在第二个循环之前更改添加options.clear()
的代码,这样选项ObservableList就会重置,我可以从0开始添加元素,然后将它们添加到h
组合框中。
问题是我实际上没有得到插入的imageView。
我也试着移动
listaComboBox.get(h).setItems(options);
listaComboBox.get(h).setCellFactory(c -> new StatusListCell());
在第二个for
周期之外,但我仍然什么也得不到。
你知道问题出在哪里吗?我已经尝试了这么久,但我仍然不能弄清楚真正的问题是什么。
发布于 2018-06-12 02:11:14
你总是添加到同一个列表中。但是,对于不同的项目列表,您需要为每个ComboBox
使用不同的ObservableList
。
此外,应该避免重新加载图像和重新创建ImageView
,以提高程序的效率:
//Arraylist made of arraylist containing the data I need
ArrayList<ArrayList<Dice>> roundTrackData = gameManager.roundTrack.getDices();
System.out.println(roundTrackData);
final Map<String, WeakReference<Image>> cache = new HashMap<>();
for(int h = 0; h < roundTrackData.size(); h++) {
ComboBox<String> combo = listaComboBox.get(h);
combo.setCellFactory(c -> new StatusListCell(cache));
ObservableList<String> options = FXCollections.observableArrayList();
List<Dice> testing = roundTrackData.get(h);
System.out.println(" Testing:" + testing);
System.out.println(" Testing size:" + testing.size());
for(Dice die : testing){
System.out.println(" Inside cicle ");
String color = Character.toString(die.getColor());
String value = die.getValue().toString();
String diceRound = value+color+".png";
options.add(diceRound);
//listaComboBox is an array list containing 10 comboboxes
System.out.println("Dice color "+ color);
System.out.println("Dice value"+ value);
}
combo.setItems(options);
}
public class StatusListCell extends ListCell<String> {
private final Map<String, WeakReference<Image>> cache;
private final ImageView imageView;
public StatusListCell(Map<String, WeakReference<Image>> cache) {
imageView = new ImageView();
imageView.setFitWidth(40);
imageView.setFitHeight(40);
setGraphic(imageView); // keep image even if empty for constant cell size
this.cache = cache;
}
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText("");
imageView.setImage(null); // don't display a image
} else {
cache.compute(item, (k, v) -> {
// retrieve image or load
Image img = null;
if (v != null) {
img = v.get();
}
if (img == null) {
img = new Image(k);
v = new WeakReference<>(img);
}
// change image in imageview
imageView.setImage(img);
return v;
});
setText("a");
}
}
}
https://stackoverflow.com/questions/50802530
复制相似问题