JavaFX:如何获取在虚拟流中显示的表视图的行数

时间:2015-02-26 04:25:51

标签: java javafx row tableview

我想获取当前表视图虚拟流的行数 例如,我有一个1000行的表视图,表视图显示每页只有50行,如果表视图响应,如何获得这个“50”,可以是 重新调整大小。

总结一下,我希望保持我的表格视图行正确显示,没有一半的行被隐藏,另一半可见

请帮助我,并提前致谢

这是一个问题的例子:

            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);
                }
            }

1 个答案:

答案 0 :(得分:0)

要获取当前可见行数,可以包含以下内容并调用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 );
  }