gpt4 book ai didi

JavaFX:如何获取虚拟流中显示的 TableView 的行数

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:11 25 4
gpt4 key购买 nike

我想获取 TableView 当前虚拟流的行数例如,我有一个 1000 行的 TableView , TableView 每页仅显示 50 行,如果 TableView 具有响应性并且可以是,如何获取此“50”重新调整大小。

夏天,我想让我的 TableView 行正确显示,没有行的一半被隐藏而另一行可见

请帮助我,提前致谢

这是一个问题示例:

            package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {

//This is table view that will view model information
TableView<Person> table = new TableView<>();

//First name column to view first name attribute of person model
TableColumn<Person, String> firstName = new TableColumn<>("First Name");
firstName.setCellValueFactory(new PropertyValueFactory<>("firstName"));

//Last name column to view first name attribute of person model
TableColumn<Person, String> lastName = new TableColumn<>("Last Name");
lastName.setCellValueFactory(new PropertyValueFactory<>("lastName"));

//Add this columns to table view
table.getColumns().add(firstName);
table.getColumns().add(lastName);

//Set dummy list of person to table view
for (int i = 0; i < 1000; i++) {
table.getItems().add(new Person("First Name" + i, "Last Name" + i));
}

//create TextField and bind what i want to its text property
//And what i want is : Number of rows that show on table virtual flow, by other words
//that number of rows that is visible to me now according to table height
TextField box = new TextField();
// ?????????????????
// ?????????????????

//Add table and box to border pane
BorderPane root = new BorderPane();
root.setCenter(table);
root.setBottom(box);

Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();

} catch(Exception e) {
e.printStackTrace();
}
}

//This is model class
public class Person{
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

public static void main(String[] args) {
launch(args);
}
}

最佳答案

要获取当前可见行数,您可以包含以下内容并调用 getNumberOfVisibleRows():

  private int getNumberOfVisibleRows()
{
VirtualFlow<?> vf = loadVirtualFlow();
return vf.getLastVisibleCell().getIndex() - vf.getFirstVisibleCell().getIndex();
}


private VirtualFlow<?> loadVirtualFlow()
{
return (VirtualFlow<?>) ( (TableViewSkin<?>) table.getSkin() ).getChildren().get( 1 );
}

关于JavaFX:如何获取虚拟流中显示的 TableView 的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com