有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JavaFX 11可编辑组合框引发IndexOutOfBoundsException

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

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

有谁能建议是什么导致了这个异常

非常感谢

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();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    查看堆栈跟踪的[开始部分]

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

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