gpt4 book ai didi

java - 从表中选择行

转载 作者:行者123 更新时间:2023-12-01 14:23:37 25 4
gpt4 key购买 nike

尝试获取 TableView 中所选行的第一列值时遇到一些问题。我动态填充表数据:

 private TableView<Category> viewCategory = new 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;
}

public class Category {

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

当选择该行时,我在表格 View 上执行此操作:

viewCategory.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
// this method will be called whenever user selected row
@override
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
}
);

但是,我收到错误消息:

<anonymous shopManagement.boundary.RetrieveProductUI$1> is not abstract and does not override abstract method changed(ObservableValue,Object,Object) in ChangeListener

我不知道为什么。有人可以帮忙吗?

更新部分

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

我设法显示数据,但无法获取所选行。它给了我一条错误消息:

IndexOutOfBoundsException: Index: 0, Size: 0

最佳答案

YourChangeListener 不完整。您有 @override 注释,但没有覆盖任何方法。你的changeListener需要像这样

    t.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue, String s, String s2) {

}
});

关于java - 从表中选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17337838/

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