【问题标题】:How can I get JavaFx Combobox to respond to user typing?如何让 JavaFx Combobox 响应用户输入?
【发布时间】:2018-08-24 00:08:24
【问题描述】:

我正在编写一个程序,该程序涉及让用户在组合框中键入内容,并且项目列表应更改以反映框中的文本(类似于您在 Google 中键入时的自动完成)

但是,在我按下 Enter 之前,我的组合框不会更新。键入常规键时,它似乎没有更新。我尝试将各种侦听器添加到组合框中,但没有一个能解决问题。

这是最成功的代码sn-p。从 fxml 代码调用它:onKeyReleased="#keyReleased"。它可以正常工作,但仍然只有在按下 Enter 时才会执行。

public void keyReleased() throws SQLException, ClassNotFoundException
{
    String coname = custconame_combo.getValue();

    scriptHandler = new ScriptHandler();

    custconame_combo.getItems().clear();
    int i = 0;
    for (String s : scriptHandler.searchCustomer(coname))
    {
        System.out.println(s);
        custconame_combo.getItems().add(s);

        custconame_combo.show();

        i += 1;
    }
}

我搜索了高低,似乎仍然无法解决这个问题。

【问题讨论】:

  • ComboBox 有一个 editor 属性,其中包含一个 TextField。您是否尝试列出 TextFieldtext 属性以进行更改?
  • 正如@Slaw 提到的,尝试将您的听众添加到编辑器的TextField 中:comboBox.getEditor().textProperty().addListener()
  • 我确实建议谨慎使用这种设计,但是,因为每次输入键时向ComboBox 添加一个新条目可能不是预期的用户体验。仅在用户输入完整个条目后才添加新项目不是最好的吗?
  • 请提供一个minimal reproducible example 来说明问题。
  • @Zephyr 我尝试使用custphone_combo.getEditor().textProperty().addListener(...) 向文本属性添加一个侦听器,但是当我使用它时,没有执行任何操作。即使按回车键也不行。此外,我在每次按键后添加一个新条目,因为与我一起工作的人想要一个类似自动完成的功能来减少拼写错误。

标签: java javafx


【解决方案1】:

既然我已经解决了我的问题,我会分享我的发现。

第三方库提供了最简单的解决方案。我选择了JFoenix 的自动补全类。它具有我一直在寻找的功能,而且我并不觉得我在尝试重新发明轮子。

这个答案对我的搜索很有帮助:JavaFX TextField Auto-suggestions

【讨论】:

    【解决方案2】:

    刚刚遇到类似的问题。 onKeyReleased 方法不会根据需要响应。使用事件处理程序。 这是我的代码(刚刚测试过并且运行良好):

    currencySymbolComboBox.setOnKeyPressed(event -> {
            if(currencySymbolComboBox.isShowing()) {
                if(event.getCode().isLetterKey()) {
                    currencyComboBoxKeysTyped += event.getCode().getName();
    
                    Optional<String> os = currecnySymbolsObservableList.stream()
                            .filter(symbol -> symbol.startsWith(currencyComboBoxKeysTyped))
                            .findFirst();
    
                    if (os.isPresent()) {
                        int ind = currecnySymbolsObservableList.indexOf(os.get());
                        ListView<String> lv = ((ComboBoxListViewSkin) currencySymbolComboBox.getSkin()).getListView();
                        lv.getFocusModel().focus(ind);
                        lv.scrollTo(ind);
                        currencySymbolComboBox.getSelectionModel().select(ind);
                    } else {
                        currencyComboBoxKeysTyped = currencyComboBoxKeysTyped
                                .substring(0, currencyComboBoxKeysTyped.length() - 1);
                    }
                }
                else if(event.getCode() == KeyCode.BACK_SPACE) {
                    if(currencyComboBoxKeysTyped.length() > 0) {
                        currencyComboBoxKeysTyped = currencyComboBoxKeysTyped
                                .substring(0, currencyComboBoxKeysTyped.length() - 1);
                    }
                }
            }
        });
    
        currencySymbolComboBox.showingProperty().addListener((observable, oldValue, newValue) -> {
            if(!currencySymbolComboBox.isShowing()) {
                currencyComboBoxKeysTyped = "";
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 1970-01-01
      • 2015-11-10
      • 2015-10-25
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 1970-01-01
      相关资源
      最近更新 更多