选择后Javafx ComboBox消失的项目

Ben*_*ard 5 icons combobox javafx image list

我创建了(JavaFX)组合框,并在其中填充了由HBoxes制成的可观察列表,以便可以在每个列表单元格中显示带有一些文本的图像。

这显示得很好,除了事实是,只要您选择列表中的一项,它就会消失。选择每个项目后,它将根本不会渲染任何项目。(您仍然可以通过单击以前的位置来选择它们。

您知道我该如何纠正吗?

在此处输入图片说明 在此处输入图片说明

我的部分代码显示在下面:

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


}
Run Code Online (Sandbox Code Playgroud)

提前感谢你的帮助!

Ben*_*ard 5

好的,所以我已经设法解决了这个问题,感谢@James_D 的非常友好的帮助!

这适用于像我一样对 Java 文档中给出的示例有些畏惧的人。(虽然,我下面的描述可能更糟!!)


所以,我开始HBox在我想要的布局中添加一个直接进入ComboBox......这是一个坏主意

所以,在你删除你所做的一切之前,把它保存在HBox某个地方,然后执行以下操作:

1 . 创建一个新类来保存将进入每个单元格的日期(图像和字符串)。让 getter/setter 来做到这一点。我打电话给我的IconTextCell

2 . 将以下代码添加到您所在的类中ComboBox

yourComboBox.setCellFactory(new Callback<ListView<T>, ListCell<T>>() {

    @Override public ListCell<T> call(ListView<T> p) {
        return new ListCell<T>() {
            Label name = new Label();
            Label icon = new Label();
            private final HBox cell;
            { 
                setContentDisplay(ContentDisplay.GRAPHIC_ONLY); 
                cell = new HBox();

                //HERE, ADD YOUR PRE-MADE HBOX CODE

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

            @Override protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setGraphic(null);
                } else {
                    name.setText(item.getLabel());
                    icon.setGraphic(item.getIcon());
                    setGraphic(cell);
                    //HERE IS WHERE YOU GET THE LABEL AND NAME
                }
           }
      };
  }
});
Run Code Online (Sandbox Code Playgroud)

您会看到主要内容与我已经制作的内容非常相似,因此没有丢失任何代码。 只需用您自己的类替换“T”即可表示单元格。

3 . 这将在列表中显示您的图标和字符串,但您还需要显示在按钮中( 的灰色顶部选择器部分combobox,又名button)。这样做,我们需要添加以下代码:

class IconTextCellClass extends ListCell<T> {
    @Override
    protected void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        if (item != null) {
            setText(item.getLabel());
        }
    }
};

selectionBox.setButtonCell(new IconTextCellClass());
Run Code Online (Sandbox Code Playgroud)

……我就是这样做的。我希望这会有所帮助-请将其与我的原始帖子进行比较。实际内容(我在其中创建 HBox 等)显然没有概括。您可以根据需要将其设置为简单或复杂。

再次感谢您的帮助!我希望这篇文章可以帮助其他人!


Jam*_*s_D 4

这正是文档中“关于将节点插入 ComboBox 项目列表的警告”下引用的示例。

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

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