首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >组合框不使用setCellFactory添加元素

组合框不使用setCellFactory添加元素
EN

Stack Overflow用户
提问于 2018-06-12 00:45:54
回答 1查看 75关注 0票数 0

我有一个由10个组合框组成的数组列表。我想用n个图像视图加载每个组合框。

我从一个给我提供正确数据的类中获得我需要的数据。您在代码中找到的arraylist的arraylist是不同骰子之间的“模拟”,创建不同的列。例如,我有最多10列,每列由n个骰子组成。

代码如下:

代码语言:javascript
复制
        //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类:

代码语言:javascript
复制
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。

我也试着移动

代码语言:javascript
复制
listaComboBox.get(h).setItems(options);
listaComboBox.get(h).setCellFactory(c -> new StatusListCell());

在第二个for周期之外,但我仍然什么也得不到。

你知道问题出在哪里吗?我已经尝试了这么久,但我仍然不能弄清楚真正的问题是什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-12 02:11:14

你总是添加到同一个列表中。但是,对于不同的项目列表,您需要为每个ComboBox使用不同的ObservableList

此外,应该避免重新加载图像和重新创建ImageView,以提高程序的效率:

代码语言:javascript
复制
//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);
}
代码语言:javascript
复制
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");
        }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50802530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档