gpt4 book ai didi

java - 表中选定行的索引越界异常

转载 作者:行者123 更新时间:2023-11-30 01:31:59 26 4
gpt4 key购买 nike

从 TableView 中选择一行时出现索引越界异常。以下是我从数据库获取数据的方法:

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

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

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

但是,如果您只想查找最后选定的行,您可以使用:

更新:

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

关于java - 表中选定行的索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17342260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com