Question

Can you list possible reasons why calling tableView.getSelectionModel().select(0); on a javafx.scene.control.TableView named tableView won't work?

For this case, let's say I'm using cellSelectionEnabled(false) and SelectionMode.SINGLE, and tableView.getItems() returns a non-empty FXCollections.observableArrayList.

To be more precise, I'm looking for possible answers why there can be instances wherein calling tableView.getSelectionModel().select(0); from a method like selectTableViewRow0() won't work:

private void selectTableViewRow0(){
    tableView.getSelectionModel().select(0);
}

Any suggestion, guys?

Edit:
As mentioned in the comments below, calling tableView.getSelectionModel().isSelected(0); returns true. I'm guessing I can use reflection to call the code in TableView that highlights the selected row. The problem is I can't seem to find out which part of the TableView code I should call. I figured calling select(0) will lead to calling the following two lines internally. But after that there seems to be no code telling the TableView to highlight the selected row.

updateSelectedIndex(row);
focus(row, column);
Was it helpful?

Solution 2

Thanks to jewelsea and jhsheets for contributing. One reason I found for this problem is that when a similar scene comes into play, and then the old scene is not garbage collected, the old table view item (from the old scene) will be selected. Luckily, I found a related bug, which I can discuss if someone is interested.

OTHER TIPS

Have you tried

Platform.runLater(new Runnable() {
    public void run() {
        tableView.getSelectionModel().select(0);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top