首页 > 解决方案 > How to correct a display problem when inserting a row in a JavaFX grid?

问题描述

I want to insert a new JavaFX bean in a grid using an "insert" button. Everything is fine, except for a display problem. After insertion, a "ghost selection" is displayed lower in the grid, as shown in this screenshot. In this example, a fourth section bean was added and selected as requested. But a fake selection appears 10 lines under the last real bean, where no bean is set for this row.

Screenshot of a ghost selection

Has anyone experienced this kind of behavior? Any clue how get rid of this ghost selection? Here is what the code for the insert button looks like:

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

标签: javajavafx-8

解决方案


最后,添加tableView.refresh()纠正了这种奇怪的行为。没有更多的幽灵选择。


推荐阅读