簡體   English   中英

JavaFX:如何獲取虛擬流中顯示的表視圖的行數

[英]JavaFX: How to get number of rows of table view that are showing in virtual flow

我想獲取表視圖的當前虛擬流的行數例如,我有一個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);
                }
            }

要獲取當前的可見行數,可以包括以下內容並調用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 );
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM