通过在文本框中输入ID来搜索javafx表视图的功能:
searchParts.setOnAction(e -> {
Integer searchId = Integer.parseInt(txtsearchParts.getText());
partsTable.getItems().stream().filter(part -> part.getPartID()==searchId).findAny();
partsTable.getItems().stream()
.filter(part -> part.getPartID() == searchId)
.findAny()
.ifPresent(part -> {
partsTable.getSelectionModel().select(part);
partsTable.scrollTo(part);
});
});
答案 0 :(得分:0)
试试这个
searchParts.setOnAction(e -> {
Integer searchId = Integer.parseInt(txtsearchParts.getText());
// copy / paste error ?
//partsTable.getItems().stream()
// .filter(part -> part.getPartID() == searchId)
// .findAny();
Optional optional = partsTable.getItems().stream()
.filter(part -> part.getPartID() == searchId)
.findAny();
if(optional.isPresent()) {
partsTable.getSelectionModel().select(optional.get());
partsTable.scrollTo(optional.get());
}
else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.initOwner(partsTable.getScene().getWindow());
alert.initModality(Modality.APPLICATION_MODAL);
alert.setHeaderText("Item not found!!!");
alert.setContentText(null);
alert.show();
}
});