点击弹出窗口时,JavaFX 8过滤的ComboBox会抛出IndexOutOfBoundsException [英] JavaFX 8 filtered ComboBox throws IndexOutOfBoundsException when popup clicked

查看:201
本文介绍了点击弹出窗口时,JavaFX 8过滤的ComboBox会抛出IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Java 8u102。我有一个模态窗口,其中包含 Combobox ,其项目是从字符串列表创建的 FilteredList ComboBox 是可编辑的,以便用户可以输入文本(自动转换为大写)。然后过滤 ComboBox 弹出窗口中的项目,以便仅保留以输入文本开头的项目。这很有用。

I am running Java 8u102. I have a modal window that contains a Combobox whose items are a FilteredList created from a list of Strings. The ComboBox is editable so that the user can enter text (automatically converted to uppercase). The items in the ComboBox popup are then filtered such that only those items that start with the entered text remain. This works great.

问题是,当您点击过滤弹出窗口中的项目时,所选项目将在组合框编辑器中正确显示,弹出窗口将关闭,但抛出 IndexOutOfBoundsException ,可能从在该行创建窗口的代码开始 - stage.showAndWait() 。下面是运行 ComboBox 的代码。

The problem is that when you click an item in the filtered popup, the selected item will be properly displayed in the combobox editor and the popup will close, but an IndexOutOfBoundsException is thrown, probably starting in the code that created the window at the line - stage.showAndWait(). Below is the code running the ComboBox.

有关解决方案的任何建议吗?我计划在组合框中添加更多功能,但我想首先处理这个问题。谢谢。

Any suggestions for a work-around? I plan to add more functionality to the combobox, but I'd like to deal with this issue first. Thanks.

  FilteredList<String> filteredList = 
     new FilteredList(FXCollections.observableArrayList(myStringList), p -> true);
  cb.setItems(filteredList);
  cb.setEditable(true);

  // convert text entry to uppercase
  UnaryOperator<TextFormatter.Change> filter = change -> {
     change.setText(change.getText().toUpperCase());
     return change;
  };
  TextFormatter<String> textFormatter = new TextFormatter(filter);
  cb.getEditor().setTextFormatter(textFormatter);

  cb.getEditor().textProperty().addListener((ov, oldValue, newValue) -> {
     filteredList.setPredicate(item -> {
         if (item.startsWith(newValue)) {
             return true; // item starts with newValue
         } else {
            return newValue.isEmpty(); // show full list if true; otherwise no match
         }
     });
  });


推荐答案

问题与这个问题:你可以换行 textProperty 上的监听器内容到 Platform.runLater 块。

The problem is the same as in this question: You can wrap the content of the listener on the textProperty into a Platform.runLater block.

cb.getEditor().textProperty().addListener((ov, oldValue, newValue) -> {
    Platform.runLater(() -> {
        filteredList.setPredicate(item -> {
            if (item.startsWith(newValue)) 
                return true; // item starts with newValue
             else 
                return newValue.isEmpty(); // show full list if true; otherwise no match
        });
    });
});

或使用三元运营商

cb.getEditor().textProperty().addListener((ov, oldValue, newValue) -> Platform.runLater(
        () -> filteredList.setPredicate(item -> (item.startsWith(newValue)) ? true : newValue.isEmpty())));

这篇关于点击弹出窗口时,JavaFX 8过滤的ComboBox会抛出IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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