0

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

 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)

有谁知道为什么会有索引越界异常?我的代码中没有红线。

提前致谢。

4

1 回答 1

1

您可以通过 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();

但是,如果您只想 sout 最后选择的行,您可以使用:

升级版:

    //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
                    } 
                }
            }); 
于 2013-06-27T13:43:40.130 回答