选择后Javafx ComboBox消失的项目 [英] Javafx ComboBox disappearing items after select

查看:20
本文介绍了选择后Javafx ComboBox消失的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 (JavaFX) combobox,我正在用一个由 HBoxes 组成的可观察列表填充它,以便我可以在每个图像中显示一些文本列出单元格.

I have created a (JavaFX) combobox, which I am populating with an observable list made from HBoxes, so that I can display an image with some text in each list cell.

这显示得很好,除了当您选择列表中的一项时,它会消失的事实.一旦您选择了每个项目,它就不会渲染任何项目.(您仍然可以通过单击它们之前所在的空间来选择它们.

This displays well, other than the fact that whenever you select one of the items in the list, it will disappear. Once you have selected every item, it will not render any items at all. (You can still select them by clicking in the space where they previously were.

请问您知道我该如何纠正吗?

Do you know how I might correct this, please?

我的部分代码如下所示:

public class IconListComboBox {

Group listRoot = new Group();
VBox mainVBox = new VBox();
ComboBox selectionBox = new ComboBox();
List<HBox> list = new ArrayList<HBox>();
ListView<HBox> listView = new ListView<HBox>();
ObservableList<HBox> observableList;



public IconListComboBox(int dimensionX, int dimensionY, ArrayList<String> names, ArrayList<ImageView> icons)
{

    //VBox.setVgrow(list, Priority.ALWAYS);
    selectionBox.setPrefWidth(dimensionY);
    selectionBox.setPrefHeight(40);

    for(int i = 0; i < names.size(); i++)
    {
        HBox cell = new HBox();
        Label name = new Label(names.get(i));
        Label icon = new Label();
        icon.setGraphic(icons.get(i));
        name.setAlignment(Pos.CENTER_RIGHT);
        icon.setAlignment(Pos.CENTER_LEFT);
        icon.setMaxWidth(Double.MAX_VALUE);
        HBox.setHgrow(icon, Priority.ALWAYS);
        cell.getChildren().add(icon);
        cell.getChildren().add(name);
        list.add(cell);


    }

    observableList = FXCollections.observableList(list);
    listView.setItems(observableList);

    listView.setPrefWidth(dimensionX);
    selectionBox.setMaxWidth(dimensionX);
    listView.setMaxWidth(dimensionX);

    selectionBox.setItems(observableList);

    mainVBox.getChildren().add(selectionBox);
    mainVBox.getChildren().add(listRoot);
    //mainVBox.getChildren().add(listView);
    //listRoot.getChildren().add(listView);


}

预先感谢您的帮助!

推荐答案

这正是 文档 下有关将节点插入 ComboBox 项目列表的警告".

This is exactly the example cited in the documentation under "A warning about inserting Nodes into the ComboBox items list".

组合框中的项目列表应代表数据 - 而不是用于显示数据的 UI 组件.问题是 HBox 不能在场景图中出现两次:因此它不能同时出现在选定单元格"和下拉列表中的单元格中.

The list of items in the combo box should represent data - not the UI component used to display the data. The issue is that the HBox cannot appear twice in the scene graph: so it cannot appear both in the "selected cell" and as a cell in the drop-down list.

相反,创建一个类来表示您在 ComboBox 中显示的数据,并使用单元工厂来指示 ComboBox 如何显示这些数据.一定要设置一个按钮单元格(用于所选项目的单元格).

Instead, create a class that represents the data you are displaying in the ComboBox, and use a cell factory to instruct the ComboBox as to how to display those data. Be sure to set a button cell as well (the cell used for the selected item).

这篇关于选择后Javafx ComboBox消失的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆