Comment obtenir les indices des lignes visibles dans un TableView dans JavaFX 9? Dans JavaFX 8, je peux faire ce qui suit:Comment trouver les indices des lignes visibles dans un TableView dans JavaFX 9
// --- The offending imports in Java 9
// import com.sun.javafx.scene.control.skin.TableViewSkin;
// import com.sun.javafx.scene.control.skin.VirtualFlow;
/**
* This is a total hack. We need it as scrollTo jumps the selected
* row to the top of the table. Jarring if the row is already
* visible. As a workaround, we only scroll if the row isn't already
* visible
*
* @return A 2 element ray with the start and end index of visible rows
*/
public int[] getVisibleRows() {
TableView<?> tableView = getTableView();
TableViewSkin<?> skin = (TableViewSkin<?>) tableView.getSkin();
if (skin == null) return new int[] {0, 0};
VirtualFlow<?> flow = (VirtualFlow<?>) skin.getChildren().get(1);
int idxFirst;
int idxLast;
if (flow != null &&
flow.getFirstVisibleCellWithinViewPort() != null &&
flow.getLastVisibleCellWithinViewPort() != null) {
idxFirst = flow.getFirstVisibleCellWithinViewPort().getIndex();
if (idxFirst > tableView.getItems().size()) {
idxFirst = tableView.getItems().size() - 1;
}
idxLast = flow.getLastVisibleCellWithinViewPort().getIndex();
if (idxLast > tableView.getItems().size()) {
idxLast = tableView.getItems().size() - 1;
}
}
else {
idxFirst = 0;
idxLast = 0;
}
return new int[]{idxFirst, idxLast};
}
En Java 9, dans le cadre de la modularisation, l'équipe JDK se cache que de l'API ne sont pas censés être publics (par exemple, tous les paquets qui commencent par ' com.sun) Si je tente de le compiler en Java 9, je reçois des erreurs de:
[...] cannot find symbol
[ERROR] symbol: class TableViewSkin
[ERROR] location: package com.sun.javafx.scene.control.skin
[...] cannot find symbol
[ERROR] symbol: class VirtualFlow
[ERROR] location: package com.sun.javafx.scene.control.skin
est-il un moyen officiel pour obtenir les lignes visibles dans un TableView? Des idées sur un autre, une meilleure solution pour cela?
MISE À JOUR: Solution basée sur @ solution de user4712734
TableViewExt<?> tableViewExt = new TableViewExt<>(tableView);
public int[] getVisibleRows() {
return new int[]{ tableViewExt.getFirstVisibleIndex(),
tableViewExt.getLastVisibleIndex()};
}
Merci! Je vais faire un test sur cette semaine. – hohonuuli
Cela a fait l'affaire !! Les commentaires com.sun.javafx sont utiles dans votre réponse pour illustrer ce que vous avez fait. Mais j'ai posté une version nettoyée à https://gist.github.com/hohonuuli/29dba349e6cf7d55bdb9d17e97392720 pour ceux qui veulent juste le code. – hohonuuli