Differences between ComboBox and ChoiceBox in JavaFX

Slayer0248 picture Slayer0248 · Nov 8, 2015 · Viewed 29.7k times · Source

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.

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?

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?

Answer

James_D picture James_D · Nov 8, 2015

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

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.