Scroll TableView#滚动到(int索引),但在底部显示行
我想向下滚动到Scroll TableView#滚动到(int索引),但在底部显示行,scroll,javafx,tableview,Scroll,Javafx,Tableview,我想向下滚动到表视图中的特定行TableView#scrollTo(int-index)将指定索引上的行放在视图的顶部,但我希望指定索引位于视图的底部。这是因为对于我来说,指定的行始终是当前关注行下面的行,我只想根据需要滚动显示下一行。即:我希望您在使用向上键和向下键更改行选择时获得的行为。这可能吗?这是一个MCVE: import javafx.application.Application; import javafx.beans.property.SimpleStringProperty;
表视图中的特定行TableView#scrollTo(int-index)
将指定索引上的行放在视图的顶部,但我希望指定索引位于视图的底部。这是因为对于我来说,指定的行始终是当前关注行下面的行,我只想根据需要滚动显示下一行。即:我希望您在使用向上键和向下键更改行选择时获得的行为。这可能吗?这是一个MCVE:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestScrollToTable extends Application {
@Override
public void start(Stage stage) {
TableView<ObservableList<String>> table = new TableView<>();
table.getSelectionModel().setCellSelectionEnabled(true);
ObservableList<String> columns = FXCollections.observableArrayList();
for (int i = 0; i < 10; i++) {
columns.add("column " + i);
}
ObservableList<ObservableList<String>> data = FXCollections.observableArrayList();
for (int rowIndex = 0; rowIndex < 100; rowIndex++) {
ObservableList<String> row = FXCollections.observableArrayList();
for (int colIndex = 0; colIndex < columns.size(); colIndex++) {
row.add("row" + rowIndex + "col" + colIndex);
}
data.add(row);
}
for (int colIndex = 0; colIndex < columns.size(); colIndex++) {
final int j = colIndex;
TableColumn<ObservableList<String>, String> col = new TableColumn<ObservableList<String>, String>(
columns.get(colIndex));
col.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get(j)));
table.getColumns().add(col);
}
table.setItems(data);
Button scrollToButton = new Button("Focus next row");
scrollToButton.setOnAction(e -> {
if (table.getFocusModel().getFocusedItem() != null) {
int nextIndex = table.getFocusModel().getFocusedIndex()+1;
table.getFocusModel().focus(nextIndex);
table.scrollTo(nextIndex);
}
});
VBox root = new VBox();
root.getChildren().addAll(table, scrollToButton);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch();
}
}
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类TestScrollToTable扩展了应用程序{
@凌驾
公众假期开始(阶段){
TableView table=新TableView();
table.getSelectionModel().setCellSelectionEnabled(true);
ObservableList columns=FXCollections.observableArrayList();
对于(int i=0;i<10;i++){
列。添加(“列”+i);
}
ObservableList data=FXCollections.observableArrayList();
对于(int-rowIndex=0;rowIndex<100;rowIndex++){
ObservableList行=FXCollections.observableArrayList();
对于(int colIndex=0;colIndexnewSimpleStringProperty(param.getValue().get(j));
table.getColumns().add(col);
}
表2.设置项目(数据);
按钮滚动按钮=新按钮(“聚焦下一行”);
scrollToButton.setOnAction(e->{
if(table.getFocusModel().getFocusedItem()!=null){
int-nextIndex=table.getFocusModel().getFocusedIndex()+1;
table.getFocusModel().focus(nextIndex);
表1.scrollTo(下一个索引);
}
});
VBox root=新的VBox();
root.getChildren().addAll(表,滚动按钮);
舞台场景(新场景(根));
stage.show();
}
公共静态void main(字符串[]args){
发射();
}
}
您可以使用此功能
private void scrollToUpAndBottom(int indexToScroll, VirtualFlow vf) {//scroll to up
if (indexToScroll < vf.getFirstVisibleCell().getIndex()) {
TableView.this.scrollTo(indexToScroll);
} else if (indexToScroll > vf.getLastVisibleCell().getIndex()) {//scroll to down
int delta = vf.getLastVisibleCell().getIndex() - vf.getFirstVisibleCell().getIndex();
TableView.this.scrollTo(indexToScroll - delta + 2); // +2 is margin
}
}
public VirtualFlow getVirtualFlow(){ return flow;}