JavaFX - 过滤的ComboBox [英] JavaFX - Filtered ComboBox

查看:195
本文介绍了JavaFX - 过滤的ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个ComboBox,它按照用户类型过滤列表项。它应该如下工作:

I want a ComboBox, that filteres the list items as the user types. It should work as follow:


  • 键入时,文本字段应显示一个可能的选择,但用户没有的单词部分但是应该突出显示键入的内容。

  • 当他打开列表时,下拉菜单应该只显示可能的选项?

  • 使用箭头键,用户应该选择其中一个缩小可能的项目后的项目。

  • 过滤并不重要,跳到第一个匹配的选择也没关系。

  • When typing, the textfield should show one possible selection, but the part of the word that the user has not yet typed should be highlighted.
  • When he opens the list, the dropdown menu should only show possible options?
  • Using the arrow keys, the user should select one of the remaining items after having narrow the possible items.
  • Filtering is not that important, jumpong to the first matching selection would be okay as well.

有没有可用的东西?

推荐答案

就过滤掉落关注。是不是在 FilteredList 中包装可能选项列表的最佳解决方案?

As far as the filtering of the drop down is concerned. Isn't wrapping the list of possible options in a FilteredList the best solution?

MCVE:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class MCVE extends Application {
    public void start(Stage stage) {
        HBox root = new HBox();

        ComboBox<String> cb = new ComboBox<String>();
        cb.setEditable(true);

        // Create a list with some dummy values.
        ObservableList<String> items = FXCollections.observableArrayList("One", "Two", "Three", "Four", "Five", "Six",
                "Seven", "Eight", "Nine", "Ten");

        // Create a FilteredList wrapping the ObservableList.
        FilteredList<String> filteredItems = new FilteredList<String>(items, p -> true);

        // Add a listener to the textProperty of the combobox editor. The
        // listener will simply filter the list every time the input is changed
        // as long as the user hasn't selected an item in the list.
        cb.getEditor().textProperty().addListener((obs, oldValue, newValue) -> {
            final TextField editor = cb.getEditor();
            final String selected = cb.getSelectionModel().getSelectedItem();

            // This needs run on the GUI thread to avoid the error described
            // here: https://bugs.openjdk.java.net/browse/JDK-8081700.
            Platform.runLater(() -> {
                // If the no item in the list is selected or the selected item
                // isn't equal to the current input, we refilter the list.
                if (selected == null || !selected.equals(editor.getText())) {
                    filteredItems.setPredicate(item -> {
                        // We return true for any items that starts with the
                        // same letters as the input. We use toUpperCase to
                        // avoid case sensitivity.
                        if (item.toUpperCase().startsWith(newValue.toUpperCase())) {
                            return true;
                        } else {
                            return false;
                        }
                    });
                }
            });
        });

        cb.setItems(filteredItems);

        root.getChildren().add(cb);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

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

这篇关于JavaFX - 过滤的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆