Improper display behavior when scrolling to a selected row in a tableview.
The code attempts to find a string in a TableView and select the row or rows containing the string. The code worked as expected. Then, the code was enhanced to scroll to the first row containing the string. However, after adding the scroll to logic, the selected rows did not behave properly.
At times, the wrong rows are selected, and at times, a row that should be selected is not. However, as soon as you click on the tableview the selected rows display properly. Or, if you mouse over a row that is supposed to be selected, then it becomes selected as you mouse over it.
If you remove the scrollTo call, then the selected rows display properly. Of course, in this case, you have to click on the tableview to manually scroll to the selected rows, and clicking on the tableview solves the display problem anyway. So not sure if this proves anything.
However, after removing the scrollTo call, if you manually scroll to the row first, then perform the find, then the selected rows appear properly, so this implies that the scrollTo call is breaking the selection model. Then if you put the scrollTo call back in, then manually scroll to the row, then perform the find, then the selected row does not appear. If I mouse over the row, then it appears.
Here's a simplified version of the code. If you remove "tableView.scrollTo(i);", then the selection works. When you add "tableView.scrollTo(i);", then it scrolls to the selected row, but the row is not selected, until you mouse over it, or click on the tableview. Experimenting with tableView.requestFocus() did not help. Also, changing the order of the scrollTo and select methods did not help. Also, using Platform.runLater() did not help. Similar find next and find all logic also produced improper display behavior of the selected rows.
buttonFind.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
int rowFound = findTable(tableView, textFieldFind.getText());
if (rowFound == -1) {
message.setText("Not found");
textFieldFind.requestFocus();
}
else {
message.setText("String found");
tableView.getSelectionModel().select(rowFound);
tableView.scrollTo(rowFound);
tableView.requestFocus();
}
}
});
private int findTable(TableView tableView, String find) {
int numRows = tableView.getItems().size();
int numCols = tableView.getColumns().size();
for (int i=0; i < numRows; i++) {
for (int j=0; j < numCols; j++) {
if (((Alarm) tableView.getItems().get(i)).get()[j].contains(find)) {
return(i);
}
}
}
return(-1);
}
The code attempts to find a string in a TableView and select the row or rows containing the string. The code worked as expected. Then, the code was enhanced to scroll to the first row containing the string. However, after adding the scroll to logic, the selected rows did not behave properly.
At times, the wrong rows are selected, and at times, a row that should be selected is not. However, as soon as you click on the tableview the selected rows display properly. Or, if you mouse over a row that is supposed to be selected, then it becomes selected as you mouse over it.
If you remove the scrollTo call, then the selected rows display properly. Of course, in this case, you have to click on the tableview to manually scroll to the selected rows, and clicking on the tableview solves the display problem anyway. So not sure if this proves anything.
However, after removing the scrollTo call, if you manually scroll to the row first, then perform the find, then the selected rows appear properly, so this implies that the scrollTo call is breaking the selection model. Then if you put the scrollTo call back in, then manually scroll to the row, then perform the find, then the selected row does not appear. If I mouse over the row, then it appears.
Here's a simplified version of the code. If you remove "tableView.scrollTo(i);", then the selection works. When you add "tableView.scrollTo(i);", then it scrolls to the selected row, but the row is not selected, until you mouse over it, or click on the tableview. Experimenting with tableView.requestFocus() did not help. Also, changing the order of the scrollTo and select methods did not help. Also, using Platform.runLater() did not help. Similar find next and find all logic also produced improper display behavior of the selected rows.
buttonFind.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
int rowFound = findTable(tableView, textFieldFind.getText());
if (rowFound == -1) {
message.setText("Not found");
textFieldFind.requestFocus();
}
else {
message.setText("String found");
tableView.getSelectionModel().select(rowFound);
tableView.scrollTo(rowFound);
tableView.requestFocus();
}
}
});
private int findTable(TableView tableView, String find) {
int numRows = tableView.getItems().size();
int numCols = tableView.getColumns().size();
for (int i=0; i < numRows; i++) {
for (int j=0; j < numCols; j++) {
if (((Alarm) tableView.getItems().get(i)).get()[j].contains(find)) {
return(i);
}
}
}
return(-1);
}