JavaFX 中 ComboBox 和 ChoiceBox 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33599322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:48:53  来源:igfitidea点击:

Differences between ComboBox and ChoiceBox in JavaFX

javauser-interfacejavafxcomboboxdifference

提问by Slayer0248

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

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

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.

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

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?

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

Edit

编辑

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?

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

回答by James_D

ComboBoxsupports a cellFactorywhich allows essentially an arbitrary UI for displaying the item in each cell. ChoiceBoxdoes not have this functionality and will only display text in each cell (which you can configure using a converter).

ComboBox支持 ,cellFactory它本质上允许使用任意 UI 来显示每个单元格中的项目。ChoiceBox没有此功能,只会在每个单元格中显示文本(您可以使用 进行配置converter)。

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

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

回答by Elltz

Well ChoiceBoxis of the idea showing you optional choices, and ComboBoxwell shows you a list of items, ChoiceBoxis like ComboBoxbut ComboBoxis for a really lengthy list as you can specify the number of items to display like 10 or more or less, but ChoiceBoxdoes not have the option it list all options and if its very long you wouldn't like the look.

WellChoiceBox是向您展示可选选项的想法,并向ComboBox您展示一个项目列表,ChoiceBox就像ComboBoxComboBox对于一个非常冗长的列表,因为您可以指定要显示的项目数量,例如 10 个或更多或更少,但ChoiceBox没有选项它列出了所有选项,如果它很长,你会不喜欢它的外观。

in short ChoiceBox, for small set of list less than 10, for more ComboBox

简而言之,ChoiceBox,对于小于 10 个的小集合,对于更多的 ComboBox

That is from my perspective the difference, as for styling you can style all.

从我的角度来看,这是不同的,至于样式,您可以设置所有样式。

回答by Bhniy Andrew

Combo BoxA combo box is a typical element of a user interface that enables users to choose one of several options. A combo box is helpful when the number of items to show exceeds some limit, because it can add scrolling to the drop down list, unlike a choice box. If the number of items does not exceed a certain limit, developers can decide whether a combo box or a choice box better suits their needs.

组合框组合框是用户界面的典型元素,使用户能够从多个选项中选择一个。当要显示的项目数量超过某个限制时,组合框很有用,因为它可以向下拉列表添加滚动,这与选择框不同。如果项目的数量没有超过一定的限制,开发人员可以决定是组合框还是选择框更适合他们的需求。

Choice BoxThis chapter describes choice boxes, the UI controls that provide support for quickly selecting between a few options.

选择框本章介绍选择框,这些 UI 控件为在几个选项之间快速选择提供支持。

http://docs.oracle.com/javafx/2/ui_controls/jfxpub-ui_controls.htm

http://docs.oracle.com/javafx/2/ui_controls/jfxpub-ui_controls.htm

回答by AHAMED AAQIB

We can simply differentiate ComboBoxand ChoiceBoxby their functionalities.Just have a look on deffintion.

我们可以简单区分 ComboBox,并ChoiceBox通过他们的functionalities.Just对deffintion看看。

The JavaFX ComboBoxcontrol enables users to choose an option from a predefined listof choices, or type in another valueif none of the predefined choices matches what the user want to select.

JavaFXComboBox控件使用户能够从预定义的选项列表中选择一个选项,或者如果预定义的选项与用户想要选择的内容都不匹配,则输入另一个值

The JavaFX ChoiceBoxcontrol enables users to choose an option from a predefined listof choices only.

JavaFX的ChoiceBox控制使用户能够选择从一个选项预定义列表中选择

回答by golimar

Apart from the mentioned differences:

除了上面提到的区别:

  • ComboBox can show a prompt with setPromptText(ChoiceBox doesn't provide that method)
  • ComboBox can show more than 10 rows with setVisibleRowCount(ChoiceBox doesn't provide that method)
  • ComboBox 可以显示提示setPromptText(ChoiceBox 不提供该方法)
  • ComboBox 可以显示超过 10 行setVisibleRowCount(ChoiceBox 不提供该方法)