JavaFX 11 可编辑组合框抛出 IndexOutOfBoundsException

标签 java javafx-11

我一直在努力为可编辑的 ComboBox 实现事件监听器,它将编辑的项目添加到应用程序列表的末尾,但这会抛出 IndexOutOfBoundsException提交编辑的项目时。我不明白为什么会这样。

因此,我创建了一个简化的应用程序,如下所示,并尝试将添加编辑项的代码包含在 Platform.runLater() block 中,但问题仍然存在。

任何人都可以提出可能导致此异常的原因吗?

非常感谢。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ComboBoxTest extends Application {
    ComboBox<String> cbItems;
    Label lblResponse;

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

    public void start(Stage stage) {
        stage.setTitle("ComboBox Demo");
        FlowPane root = new FlowPane(10, 10);
        root.setAlignment(Pos.TOP_CENTER);
        Scene scene = new Scene(root, 240, 120);
        stage.setScene(scene);
        lblResponse = new Label();
        ObservableList<String> items =
                FXCollections.observableArrayList("item1", "item2", "item3", "item4", "item5");
        cbItems = new ComboBox<String>(items);
        cbItems.setValue("item1");
        cbItems.setEditable(true);
        lblResponse.setText("Selected item is " + cbItems.getValue());

        // Listen for action events on the combo box.
        cbItems.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent ae) {
                lblResponse.setText("Selected item is " + cbItems.getValue());

                //add the modified item to the end of the list
                int i = cbItems.getSelectionModel().getSelectedIndex();
                if(!items.get(i).equals(cbItems.getValue())){
                    items.add(cbItems.getValue());
                }
            }
        });
        root.getChildren().addAll(cbItems, lblResponse);
        stage.show();
    }
}

最佳答案

查看堆栈跟踪的 [开始]...

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 5
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:359)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at javafx.base/com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
    at jfxprjct/jfxtests.ComboBoxTest$1.handle(ComboBoxTest.java:52)

现在查看文件 ComboBoxTest.java 中的第 52 行。
(请注意,它可能是堆栈跟踪中的不同行号。)
对我来说,这是第 52 行

if(!items.get(i).equals(cbItems.getValue())){

也就是说,i的值为-1(减一)。并且 iif 语句之前的行中被分配了一个值。

int i = cbItems.getSelectionModel().getSelectedIndex();

也就是说,没有没有选择的索引。所以你应该首先检查 i 的值,而不是假设它是一个有效的索引值。

然而,更好的条件(在我看来)是

if(!cbItems.getItems().contains(cbItems.getValue())){

那么你根本不需要选择的索引。

关于JavaFX 11 可编辑组合框抛出 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66062626/

相关文章:

java - Firebase 连接检查 Android 中的在线离线状态

JavaFX 11 : IllegalAccessError when creating Label

java - 如何使用JRE部署JavaFX 11 Desktop应用程序

maven - 使用来自 maven 的 JavaFX11 构建可执行 JAR

java - 没有更多便捷的Java/Java FX 11应用程序了吗?

java - 如何在超过限制时打印 Java int 值

java - 清理 finalize() 或 finally() 中的代码?

JAVAFX 舞台会根据场景的变化而改变其大小。或者这就是我的想法

java - 运行 servlet 时出现问题

java - 字符串分割返回空指针异常