ComboBox概述

The ComboBox class creates a control that allows the user to select an option from a drop-down list of options. The drop-down list appears when the user clicks on the ComboBox control. When the number of options exceeds the size of the drop-down window, the user can scroll down to further options. This differs from the ChoiceBox which is primarily used when the number of choices is a relatively small set.

ComboBox类创建一个控件,该控件允许用户从选项的下拉列表中选择一个选项。 当用户单击ComboBox控件时,将显示下拉列表。 当选项数量超过下拉窗口的大小时,用户可以向下滚动到其他选项。 这与ChoiceBox不同,后者主要用于选择数量相对较小的集合。

进口声明 ( Import Statement )


javafx.scene.control.ComboBox

建设者 ( Constructors )

The ComboBox class has two constructors depending on whether you want to create an empty ComboBox object or one populated with items.

ComboBox类具有两个构造函数,具体取决于您要创建一个空的ComboBox对象还是一个要填充项的对象。

To Create an Empty ComboBox

创建一个空的组合框

ComboBox fruit = new ComboBox();

To create a ComboBox object and populate it with String items from an ObservableList

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:

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:

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

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:

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);
}
});

翻译自: https://www.thoughtco.com/combobox-overview-2033930

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值