Как заполнить combobox javafx
On top of ComboBoxBase, the ComboBox class introduces additional API. Most importantly, it adds an items property that works in much the same way as the ListView items property. In other words, it is the content of the items list that is displayed to users when they click on the ComboBox button.
- The value property is not constrained to items contained within the items list — it can be anything as long as it is a valid value of type T.
- If the value property is set to a non-null object, and subsequently the items list is cleared, the value property is not nulled out.
- Clearing the selection in the selection model does not null the value property — it remains the same as before.
- It is valid for the selection model to have a selection set to a given index even if there is no items in the list (or less items in the list than the given index). Once the items list is further populated, such that the list contains enough items to have an item in the given index, both the selection model SelectionModel.selectedItemProperty() and value property will be updated to have this value. This is inconsistent with other controls that use a selection model, but done intentionally for ComboBox.
By default, when the popup list is showing, the maximum number of rows visible is 10, but this can be changed by modifying the visibleRowCount property. If the number of items in the ComboBox is less than the value of visibleRowCount , then the items size will be used instead so that the popup list is not exceedingly long.
As with ListView, it is possible to modify the selection model that is used, although this is likely to be rarely changed. This is because the ComboBox enforces the need for a SingleSelectionModel instance, and it is not likely that there is much need for alternate implementations. Nonetheless, the option is there should use cases be found for switching the selection model.
As the ComboBox internally renders content with a ListView, API exists in the ComboBox class to allow for a custom cell factory to be set. For more information on cell factories, refer to the Cell and ListCell classes. It is important to note that if a cell factory is set on a ComboBox, cells will only be used in the ListView that shows when the ComboBox is clicked. If you also want to customize the rendering of the ‘button’ area of the ComboBox, you can set a custom ListCell instance in the button cell property. One way of doing this is with the following code (note the use of setButtonCell :
Because a ComboBox can be editable , and the default means of allowing user input is via a TextField , a string converter property is provided to allow for developers to specify how to translate a users string into an object of type T, such that the value property may contain it. By default the converter simply returns the String input as the user typed it, which therefore assumes that the type of the editable ComboBox is String. If a different type is specified and the ComboBox is to be editable, it is necessary to specify a custom StringConverter .
A warning about inserting Nodes into the ComboBox items list
The recommended approach, rather than inserting Node instances into the items list, is to put the relevant information into the ComboBox, and then provide a custom cell factory . For example, rather than use the following code:
You should do the following:
Admittedly the above approach is far more verbose, but it offers the required functionality without encountering the scenegraph constraints.
JavaFX ComboBox
The JavaFX ComboBox control enables users to choose an option from a predefined list of choices, or type in another value if none of the predefined choices matches what the user want to select. The JavaFX ComboBox control is represented by the class javafx.scene.control.ComboBox . This JavaFX ComboBox tutorial will explain how to use the ComboBox class.
Creating a ComboBox
You create a ComboBox simply by creating a new instance of the ComboBox class. Here is a JavaFX ComboBox instantiation example:
Adding Choices to a ComboBox
You can add choices to a ComboBox by obtaining its item collection and add items to it. Here is an example that adds choices to a JavaFX ComboBox :
Adding a ComboBox to the Scene Graph
To make a ComboBox visible you must add it to the scene graph. This means that you must add the ComboBox to a Scene object or to some layout component which is then attached to the Scene object.
Here is an example showing how to add a JavaFX ComboBox to the scene graph:
The application resulting from running this example would look similar to this:
Making the ComboBox Editable
A ComboBox is not editable by default. That means, that by default the user cannot enter anything themselves, but only choose from the predefined list of options. To make a ComboBox editable you must call the setEditable() method of the ComboBox . Here is an example making a JavaFX ComboBox editable:
Once the ComboBox is editable the user can type in values into the ComboBox . The entered value is also read via the getValue() method as explained earlier. The following screenthot shows a JavaFX ComboBox which is editable, and with a custom value entered:

Reading the Selected Value
You can read the selected value of a ComboBox via its getValue() method. If no choice is selected, the getValue() method returns null . Here is an example of calling getValue() :
Listening for Selection
It is possible to listen for selection changes in a JavaFX ComboBox by setting an action listener on the ComboBox via its setOnAction() method. Here is an example of setting an action listener on a ComboBox which reads what value was selected in the ComboBox:
Как заполнить combobox javafx

Класс javafx.scene.control.ComboBox<T> позволяет создать выпадающий список. Данный класс типизируется типом элементов, которые будут храниться в списке.
Для создания выпадающего списка можно использовать один из конструкторов класса:
-
ComboBox() : создает пустой выпадающий список
Из методов ComboBox следуюет выделить следующие:
void setValue(T item) : устанавливает выбранный по умолчанию элемент (если не вызвать данный метод, то по умолчанию никакой элемент не будет выбран)
T item getValue() : возвращает текущий выбранный элемент
void setOnAction(EventHandler<ActionEvent> value) : устанавливает обработчик, который срабатывает при выборе элемента в списке
Как заполнить combobox javafx
On top of ComboBoxBase, the ComboBox class introduces additional API. Most importantly, it adds an items property that works in much the same way as the ListView items property. In other words, it is the content of the items list that is displayed to users when they click on the ComboBox button.
- The value property is not constrained to items contained within the items list — it can be anything as long as it is a valid value of type T.
- If the value property is set to a non-null object, and subsequently the items list is cleared, the value property is not nulled out.
- Clearing the selection in the selection model does not null the value property — it remains the same as before.
- It is valid for the selection model to have a selection set to a given index even if there is no items in the list (or less items in the list than the given index). Once the items list is further populated, such that the list contains enough items to have an item in the given index, both the selection model SelectionModel.selectedItemProperty() and value property will be updated to have this value. This is inconsistent with other controls that use a selection model, but done intentionally for ComboBox.
By default, when the popup list is showing, the maximum number of rows visible is 10, but this can be changed by modifying the visibleRowCount property. If the number of items in the ComboBox is less than the value of visibleRowCount , then the items size will be used instead so that the popup list is not exceedingly long.
As with ListView, it is possible to modify the selection model that is used, although this is likely to be rarely changed. This is because the ComboBox enforces the need for a SingleSelectionModel instance, and it is not likely that there is much need for alternate implementations. Nonetheless, the option is there should use cases be found for switching the selection model.
As the ComboBox internally renders content with a ListView, API exists in the ComboBox class to allow for a custom cell factory to be set. For more information on cell factories, refer to the Cell and ListCell classes. It is important to note that if a cell factory is set on a ComboBox, cells will only be used in the ListView that shows when the ComboBox is clicked. If you also want to customize the rendering of the ‘button’ area of the ComboBox, you can set a custom ListCell instance in the button cell property. One way of doing this is with the following code (note the use of setButtonCell :
Because a ComboBox can be editable , and the default means of allowing user input is via a TextField , a string converter property is provided to allow for developers to specify how to translate a users string into an object of type T, such that the value property may contain it. By default the converter simply returns the String input as the user typed it, which therefore assumes that the type of the editable ComboBox is String. If a different type is specified and the ComboBox is to be editable, it is necessary to specify a custom StringConverter .
A warning about inserting Nodes into the ComboBox items list
The recommended approach, rather than inserting Node instances into the items list, is to put the relevant information into the ComboBox, and then provide a custom cell factory . For example, rather than use the following code:
You should do the following:
Admittedly the above approach is far more verbose, but it offers the required functionality without encountering the scenegraph constraints.