本文介绍了JavaFX 中 ComboBox 和 ChoiceBox 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaFX 中的 ComboBox 和 ChoiceBox 有什么区别?我并不完全清楚这两个类的 Javadoc.

What are the differences between ComboBox and ChoiceBox in JavaFX? I'm not entirely clear on that just from the Javadoc for both classes.

归根结底,我需要一个可以在运行时动态重新填充的下拉控件(我在后端有一个数据库).对于我的应用程序中的所有情况,我只需要从下拉菜单中选择一项.用户也不应该能够从他们可见的屏幕上向下拉菜单添加选项.

At the end of the day, I need a dropdown control that can be repopulated dynamically at runtime (I've got a database on the backend). For all cases in my application, I only need to select one item from the dropdown menus. The user also shouldn't be able to add an option to the dropdown menu from the screens they are visible on.

我的理解是 ComboBox 允许用户将项目添加到下拉列表并允许选择多个项目,但从 Javadoc 看来,似乎可以按照我的需要设置 ComboBox,所以看起来他们'在某种程度上是可以互换的.我想在这种情况下,ComboBox 的开销比我真正需要的要多一些,但是还有什么只有 ComboBox 才能做到的事情会影响这个决定吗?

My understanding is that ComboBox allows the user to add items to the dropdown list and allows for selecting multiple items, but from the Javadoc it seems like it's possible to setup ComboBox in a way that meets my needs, so it seems like they're interchangeable to some extent. I guess ComboBox has a bit more overhead than I really need in this case, but is there anything else that only a ComboBox could do that would factor into this decision?

编辑

我想我已经回答了我自己关于关键差异的问题,那么还有什么我没有提到的可以区分 2 的吗?

I guess I kind of answered my own question on the key differences, so is there something else I've not mentioned that differentiates the 2?

推荐答案

ComboBox 支持 cellFactory 本质上允许使用任意 UI 来显示每个单元格中的项目.ChoiceBox 没有此功能,只会在每个单元格中显示文本(您可以使用 转换器).

ComboBox supports a cellFactory which allows essentially an arbitrary UI for displaying the item in each cell. ChoiceBox does not have this functionality and will only display text in each cell (which you can configure using a converter).

参见 http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/combo-box.htm#BABJCCIB 清单 16.5 是组合框中自定义单元工厂的示例.

See http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/combo-box.htm#BABJCCIB listing 16.5 for an example of a custom cell factory in a combo box.

这篇关于JavaFX 中 ComboBox 和 ChoiceBox 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 09:33