JavaFX ComboBox

104 篇文章 5 订阅

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.

JavaFX的ComboBox控件使得用户可以从预定义的列表项中选择一个数据项,如果预定义项没有符合用户需求的,用户可以自定义输入一个数据项。ComboBox控件是javafx.scene.control.ComboBox类的对象。以下内容将介绍如何使用ComboBox类。

Creating a ComboBox

You create a ComboBox simply by creating a new instance of the ComboBox class. Here is a JavaFX ComboBox instantiation example:

你可以通过以下方式创建ComboBox类的对象。

ComboBox comboBox = new ComboBox();

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 :

comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

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:

package com.jenkov.javafx.controls;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ComboBoxExperiments extends Application  {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("ComboBox Experiment 1");

        ComboBox comboBox = new ComboBox();

        comboBox.getItems().add("Choice 1");
        comboBox.getItems().add("Choice 2");
        comboBox.getItems().add("Choice 3");

        HBox hbox = new HBox(comboBox);

        Scene scene = new Scene(hbox, 200, 120);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

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:

comboBox.setEditable(true);

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():

String value = (String) comboBox.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 comboBox = new ComboBox();

comboBox.getItems().add("Choice 1");
comboBox.getItems().add("Choice 2");
comboBox.getItems().add("Choice 3");

comboBox.setOnAction((event) -> {
    int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
    Object selectedItem = comboBox.getSelectionModel().getSelectedItem();

    System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
    System.out.println("   ComboBox.getValue(): " + comboBox.getValue());
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX ComboBox是一个下拉列表框,它允许用户从预定义的选项中进行选择。关键字检索是指在ComboBox中输入关键字,然后根据关键字过滤选项,只显示与关键字匹配的选项。下面是一个简单的JavaFX ComboBox关键字检索的例子: ```java import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.ComboBox; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class ComboBoxDemo extends Application { private final ObservableList<String> data = FXCollections.observableArrayList( "Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Lemon", "Mango", "Pear"); @Override public void start(Stage stage) { ComboBox<String> comboBox = new ComboBox<>(data); TextField textField = new TextField(); textField.setPromptText("Search"); // 根据关键字过滤选项 textField.textProperty().addListener((observable, oldValue, newValue) -> { if (newValue == null || newValue.isEmpty()) { comboBox.setItems(data); } else { ObservableList<String> filteredList = FXCollections.observableArrayList(); for (String item : data) { if (item.toLowerCase().contains(newValue.toLowerCase())) { filteredList.add(item); } } comboBox.setItems(filteredList); } }); VBox root = new VBox(comboBox, textField); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } ``` 在这个例子中,我们创建了一个ComboBox和一个TextField。当用户在TextField中输入关键字时,我们根据关键字过滤选项,并将过滤后的选项设置为ComboBox的选项。这样,用户只能看到与关键字匹配的选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值