ComboBox类创建一个控件,允许用户从选项下拉列表中选择一个选项。当用户单击ComboBox控件时,将显示下拉列表。当选项数超过下拉窗口的大小时,用户可以向下滚动到其他选项。这与ChoiceBox不同,ChoiceBox主要用于选择数量相对较少的情况。...
ComboBox类创建一个控件,允许用户从选项下拉列表中选择一个选项。当用户单击ComboBox控件时,将显示下拉列表。当选项数超过下拉窗口的大小时,用户可以向下滚动到其他选项。这与ChoiceBox不同,ChoiceBox主要用于选择数量相对较少的情况。
进口声明
javafx.scene.control.ComboBox
建设者
ComboBox类有两个构造函数,具体取决于您是要创建一个空的ComboBox对象,还是要创建一个填充了项的ComboBox对象。
创建空组合框的步骤
ComboBox fruit = new ComboBox();
To create a ComboBox object and populate it with String items from an ObservableList ObservableList fruits = FXCollections.observableArrayList( "Apple", "Banana", "Pear", "Strawberry", "Peach", "Orange", "Plum");ComboBox fruit = new ComboBox(fruits);
Useful Methods
If you create an empty ComboBox object you can use the setItems method. Passing an ObservableList of objects will set the items in the Combobox. ObservableList fruits = FXCollections.observableArrayList( "Apple", "Banana", "Pear", "Strawberry", "Peach", "Orange", "Plum");fruit.setItems(fruits); If you want to add items to the ComboBox list later on you can use the addAll method of the getItems method. This will append the items to the end of the options list: fruit.getItems().addAll("Melon", "Cherry", "Blackberry"); To add an option to a particular place in the ComboBox option list use the add method of the getItems method. This method takes an index value and the value you wish to add: fruit.getItems().add(1, "Lemon");
Note: The index values of the ComboBox start at 0. For example, the above value of "Lemon" above will be inserted into the ComboBox option list at position 2 as the index passed is 1. To pre-select an option in the ComboBox options list, use the setValue method: fruit.setValue("Cherry"); If the value passed to the setValue method is not on the list, then the value will still be selected. However, it does not mean this value has been added to the list. If the user subsequently picks another value then the initial value will no longer be in the list to be selected. To get the value of the currently selected item in the ComboBox, use the getItems method: String selected = fruit.getValue().toString();
Usage Tips
The number of options normally presented by the ComboBox dropdown list is ten (unless there are less than ten items in which case it defaults to the number of items). This number can be changed by using the setVisibleRowCount method: fruit.setVisibleRowCount(25); Again, if the number of items in the list is less than the value set in the setVisibleRowCount method the ComboBox will default to displaying the number of items in the ComboBox dropdown.
Handling Events
To track the selection of items on a ComboBox object you can use the addListener method of the selectedItemProperty method of the SelectionModel to create a ChangeListener It will pick up the change events for the ComboBox: final Label selectionLabel = new Label();fruit.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() { public void changed(ObservableValue ov, String old_val, String new_val) { selectionLabel.setText(new_val); }});