我想使用“插入”按钮在网格中插入新的JavaFX bean。一切都很好,除了显示问题。插入后,“重影选择”将显示在网格的下方,如此屏幕截图所示。在此示例中,根据需要添加并选择了第四节bean。但是,伪造的选择出现在最后一个真实bean下方的10行中,该行未设置任何bean。
有人经历过这种行为吗?任何线索如何摆脱这个鬼选择?这是插入按钮的代码如下所示:
@FXML
private Button insert;
...
insert.setOnAction(event -> {
JfxBean newBean = createBean();
tableView.getItems().add(newBean);
int index = tableView.getItems().indexOf(newBean);
tableView.getSelectionModel().clearSelection();
tableView.requestFocus();
tableView.scrollTo(index);
tableView.getSelectionModel().focus(index);
tableView.getSelectionModel().select(index);
};
根据javaFX-8 documentation,SelectionModel.java
不公开任何focus()
方法。取而代之的是FocusModel.java
。因此,JVM将无法编译您提供的代码。
下面是一个可能的解决方案:
insert.setOnAction(event -> {
JfxBean newBean = createBean();
tableView.getItems().add(newBean);
int index = tableView.getItems().indexOf(newBean);
tableView.getSelectionModel().clearSelection();
tableView.requestFocus();
tableView.scrollTo(index);
// below line is the amendment
tableView.getFocusModel().focus(index);
tableView.getSelectionModel().select(index);
};