从表视图中选择行时,索引超出范围异常。这是我从数据库中获取数据的方式:

 public List<Category> populateCategoryTable() {
    List ll = new LinkedList();

    try {
        db.getConnection();
        String sql = "SELECT * FROM sm_category";
        ResultSet rs = null;
        // Call readRequest to get the result
        rs = db.readRequest(sql);

        while (rs.next()) {
            String ID = rs.getString("categoryID");
            String name = rs.getString("categoryName");
            String desc = rs.getString("categoryDescription");
            Category row = new Category();
            row.setid(ID);
            row.setname(name);
            row.setdesc(desc);
            ll.add(row);
        }
        rs.close();
    } catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("Error SQL!!!");
        System.exit(0);
    }
    return ll;
}


这就是我设置表格视图的方式:

public void setUpCategoryTable() {
    TableColumn IDCol = new TableColumn("ID");
    IDCol.setVisible(false);
    IDCol.setCellValueFactory(new PropertyValueFactory<Category, String>("ID"));

    TableColumn nameCol = new TableColumn("Name");
    nameCol.setCellValueFactory(new PropertyValueFactory<Category, String>("name"));

    TableColumn descCol = new TableColumn("Description");
    descCol.setCellValueFactory(new PropertyValueFactory<Category, String>("desc"));

    viewCategory.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    viewCategory.setEditable(false);
    viewCategory.getColumns().addAll(IDCol, nameCol, descCol);
    viewCategory.getItems().setAll(populateCategoryTable());


//添加这些代码以从表中选择行后发生错误

    viewCategory.getSelectionModel().setCellSelectionEnabled(true);
    viewCategory.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); // just in case you didnt already set the selection model to multiple selection.
    viewCategory.getSelectionModel().getSelectedCells().addListener(new ListChangeListener<TablePosition>() {
        @Override
        public void onChanged(Change<? extends TablePosition> change) {
            TableView.TableViewSelectionModel selectionModel = viewCategory.getSelectionModel();
            ObservableList selectedCells = selectionModel.getSelectedCells();
            TablePosition tablePosition = (TablePosition) selectedCells.get(0);
            int row = tablePosition.getRow(); // yields the row that the currently selected cell is in
            System.out.println(row);
        }
    });


//错误结束

公共课程类别{

    private SimpleStringProperty ID;
    private SimpleStringProperty name;
    private SimpleStringProperty desc;

    public void setid(String value) {
        IDProperty().set(value);
    }

    public String getid() {
        return IDProperty().get();
    }

    public SimpleStringProperty IDProperty() {
        if (ID == null) {
            ID = new SimpleStringProperty(this, "ID");
        }
        return ID;
    }

    public void setname(String value) {
        nameProperty().set(value);
    }

    public String getname() {
        return nameProperty().get();
    }

    public SimpleStringProperty nameProperty() {
        if (name == null) {
            name = new SimpleStringProperty(this, "name");
        }
        return name;
    }

    public void setdesc(String value) {
        descProperty().set(value);
    }

    public String getdesc() {
        return descProperty().get();
    }

    public SimpleStringProperty descProperty() {
        if (desc == null) {
            desc = new SimpleStringProperty(this, "desc");
        }
        return desc;
    }
}


但是,在添加所选行代码的垃圾后,索引超出范围。错误消息是:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:208)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel$6.get(TableView.java:1757)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel$6.get(TableView.java:1755)
at shopManagement.boundary.RetrieveProductUI$1.onChanged(RetrieveProductUI.java:101)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:291)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)


有人知道为什么会有索引超出范围的异常吗?我的代码中没有红线。

提前致谢。

最佳答案

您可以通过FXCollections创建ObservableList,如果仅使用table.getItems(),它将自动创建,因此用法table.getItems()。setAll(...)很好,您可以使用调试器轻松验证。

以下代码应有所帮助:

while(change.next(){
    if(change.wasAdded()){
        TableView.TableViewSelectionModel selectionModel = viewCategory.getSelectionModel();
        ObservableList selectedCells = selectionModel.getSelectedCells();
        TablePosition tablePosition = (TablePosition) selectedCells.get(0);
        int row = tablePosition.getRow(); // yields the row that the currently selected cell is in
        System.out.println(row);
    }
}


您也可以更改此代码:

TableView.TableViewSelectionModel selectionModel = viewCategory.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();


带有:

ObservableList selectedCells = change.getList();


但是,如果只想发送最后选择的行,则可以使用:

UPD:

    //viewCategory.getSelectionModel().setCellSelectionEnabled(true);
    //viewCategory.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    viewCategory.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number1) {
                    if ((Integer) number1 >= 0) {
                        Category cat = (Category) table.getItems().get(number1);
                        String id = cat.getid();
                        \\here you can fetch DB for aditional info and pass it somewhere
                    }
                }
            });

10-06 13:33