Java TableView没有';t刷新
我有一个用JavaFX编写的项目,我试图在没有结果的情况下刷新tableview。 我在谷歌上搜索了一下,尝试了一些我找到的例子,但仍然不起作用 我用信息填充tableview,该表中的每一行都可以通过双击该行添加新注释。此时将打开新选项卡窗格,并可以在其中添加新注释。关闭此选项卡窗格后,我希望刷新我单击的其中一个。 我一定是做错了什么。我只是不知道是什么 在我的StoreController中Java TableView没有';t刷新,java,javafx,Java,Javafx,我有一个用JavaFX编写的项目,我试图在没有结果的情况下刷新tableview。 我在谷歌上搜索了一下,尝试了一些我找到的例子,但仍然不起作用 我用信息填充tableview,该表中的每一行都可以通过双击该行添加新注释。此时将打开新选项卡窗格,并可以在其中添加新注释。关闭此选项卡窗格后,我希望刷新我单击的其中一个。 我一定是做错了什么。我只是不知道是什么 在我的StoreController中 private void populateTableView(List<Store> s
private void populateTableView(List<Store> stores) {
ObservableList<Store> data = FXCollections.observableArrayList(stores);
storeNumberColumn.setCellValueFactory(
new PropertyValueFactory<Store, String>("id"));
storePhoneColumn.setCellValueFactory(
new PropertyValueFactory<Store, String>("phoneNbr"));
chainColumn.setCellValueFactory(
new PropertyValueFactory<Store, String>("chainId"));
commentColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Store, ImageView>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Store, ImageView> p) {
Integer numberOfComments = p.getValue().getCommentsCount();
ReadOnlyObjectWrapper wrapper = null;
if (numberOfComments == 0) {
wrapper = null;
} else if (numberOfComments == 1) {
wrapper = new ReadOnlyObjectWrapper(new ImageView(COMMENT_SINGLE_FLAG_SOURCE));
} else {
wrapper = new ReadOnlyObjectWrapper(new ImageView(COMMENT_DOUBLE_FLAG_SOURCE));
}
return wrapper;
}
});
storeTable.setItems(data);
sortTable(storeTable, missedColumn);
}
@FXML
public void handleTableAction(MouseEvent event) {
if (event.getClickCount() == 2) {
showNewCommentStage();
}
}
private void showNewCommentStage() {
initCommentController();
Store store
= storeTable.getSelectionModel().selectedItemProperty().getValue();
commentController.showNewStage(commentPane, store);
}
public void showNewStage(Pane pane, Store store) {
this.store = store;
initStage(pane);
windowHandler = new WindowHandler(stage);
effectHandler.playEffect(pane);
constructCommentHeaders();
List<Comment> comments;
comments = commentService.listByStoreId(store.getId());
populateCommentTable(comments);
}
CommentEntity.Class
public abstract class CommentEntity {
private int commentsCount;
public int getCommentsCount() {
return commentsCount;
}
public void setCommentsCount(int commentsCount) {
this.commentsCount = commentsCount;
}
public abstract String getCommentIdentifier();
}
感谢您的输入,我甚至没有考虑ImageView/String。两个问题:
首先,您需要区分列中的单元格显示的数据和实际显示这些数据的单元格。cellValueFactory
确定显示的数据。PropertyValueFactory
是一个引用JavaFX属性的cellValueFactory
实现,因此当您调用
storeNumberColumn.setCellValueFactory(new PropertyValueFactory<Store, String>("id"));
对于cellValueFactory
,您只需使用
commentColumn.setCellValueFactory(new PropertyValueFactory<>("commentsCount"));
第二个问题实际上是关于更新。TableView
通过观察cellValueFactory
作为observeValue
s提供的内容,使其内容保持“活动”。如果在显示表时该值可能会更改,则必须提供可以观察到的实际属性:使用ReadOnlyObjectWrapper
是不好的(因为它是只读的,所以它的包装值不会更改)。如果您没有JavaFX属性访问器方法(即,如果它仅使用getXXX()
方法访问数据),则PropertyValueFactory
还将返回一个ReadOnlyObjectWrapper
。因此,您的模型类必须提供JavaFX属性
您可以通过更新CommentEntity
以使用IntegerProperty
来立即修复此问题:
public abstract class CommentEntity {
private final IntegerProperty commentsCount = new SimpleIntegerProperty();
public final int getCommentsCount() {
return commentsCountProperty().get();
}
public final void setCommentsCount(int commentsCount) {
commentsCountProperty().set(commentsCount);
}
public IntegerProperty commensCountProperty() {
return commentsCount ;
}
public abstract String getCommentIdentifier();
}
我还强烈建议更新
存储
类,以便以类似的方式使用JavaFX属性。何时以及如何编辑注释表列值?tableview如何知道新注释已添加到它所呈现的其中一个项目中?commentColumn的类型是什么?调用setCellValueFactory
中的参数类型实际上不匹配:TableColumn
的签名是setCellValueFactory(回调工厂)
。但是您有一个回调
,这没有意义。此列是T
ImageView
还是String
?如果你展示你的Store
类,它可能也会有所帮助。我已经在上面添加了Store类。我还测试了在@James_D对ImageView和String的不一致性进行反思之后,将字符串更改为ImageView。这似乎没有什么区别,图像正在显示。通过双击tableview中的一行添加注释。此时将打开一个新窗格,并在其中将注释添加到数据库中。当我关闭评论窗格时,我对db进行了一个新的查询,现在我调用了populateTableView。一切正常,但似乎没有调用调用函数。
commentColumn.setCellValueFactory(new PropertyValueFactory<>("commentsCount"));
commentColumn.setCellFactory(new Callback<TableColumn<Store, Number>, new TableCell<Store, Number>>() {
@Override
public TableCell<Store, Number>() {
private ImageView imageView = new ImageView();
@Override
public void updateItem(Number numberOfComments, boolean empty) {
super.updateItem(count, empty) ;
if (empty) {
setGraphic(null);
} else {
if (numberOfComments.intValue() == 0) {
setGraphic(null);
} else if (numberOfComments.intValue() == 1) {
imageView.setImage(new Image(COMMENT_SINGLE_FLAG_SOURCE));
setGraphic(imageView);
} else {
imageView.setImage(new Image(COMMENT_DOUBLE_FLAG_SOURCE));
setGraphic(imageView);
}
}
}
}
});
public abstract class CommentEntity {
private final IntegerProperty commentsCount = new SimpleIntegerProperty();
public final int getCommentsCount() {
return commentsCountProperty().get();
}
public final void setCommentsCount(int commentsCount) {
commentsCountProperty().set(commentsCount);
}
public IntegerProperty commensCountProperty() {
return commentsCount ;
}
public abstract String getCommentIdentifier();
}