具有复杂TableColumn的JavaFX TableView的性能问题

时间:2018-04-16 08:56:15

标签: performance javafx tableview javafx-8

我正在尝试使用“塔形”列创建JavaFX TableView。但是当TableView中有超过1000列时,性能非常差。

这是我的代码:

public class PaneDemo extends Application {

    private TableView<String> table = new TableView<>();
    private Long timeStart;
    private Long timeEnd;

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

    private static int getTotal(int layer) {
        int total = 0;
        int start = 1;
        for (int i = 0; i < layer - 1; i++) {
            start *= 5;
            total += start;
        }
        System.out.println("Total Columns: " + (total + start * 5));
        return total;
    }

    @Override
    public void start(Stage stage) {

        Scene scene = new Scene(new Group());
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setPrefWidth(900);
        anchorPane.setPrefHeight(600);
        table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
        new Thread(() -> {
            timeStart = System.currentTimeMillis();
            //init first layer
            TableColumn<String, String> Test1 = new TableColumn<>("Test1");
            TableColumn<String, String> Test2 = new TableColumn<>("Test2");
            TableColumn<String, String> Test3 = new TableColumn<>("Test3");
            TableColumn<String, String> Test4 = new TableColumn<>("Test4");
            TableColumn<String, String> Test5 = new TableColumn<>("Test5");

            Queue<TableColumn<String, ?>> queue = new LinkedList<>();
            table.getColumns().addAll(Test1, Test2, Test3, Test4, Test5);
            table.getItems().add("test");
            queue.addAll(table.getColumns());

            int index = 0;
            // set the layer of the column tower
            int temp = getTotal(4);
            while (index < temp) {
                TableColumn<String, ?> root = queue.poll();
                TableColumn<String, String> test1 = new TableColumn<>("test1");
                TableColumn<String, String> test2 = new TableColumn<>("test2");
                TableColumn<String, String> test3 = new TableColumn<>("test3");
                TableColumn<String, String> test4 = new TableColumn<>("test4");
                TableColumn<String, String> test5 = new TableColumn<>("test5");
                root.getColumns().addAll(test1, test2, test3, test4, test5);
                queue.addAll(root.getColumns());
                index++;
            }

            while (!queue.isEmpty()) {
                generateCellFactory((TableColumn<String, String>) queue.poll());
            }

            table.prefHeightProperty().bind(anchorPane.heightProperty());
            table.prefWidthProperty().bind(anchorPane.widthProperty());
            anchorPane.getChildren().add(table);
            ((Group) scene.getRoot()).getChildren().addAll(anchorPane);
            stage.setScene(scene);
            stage.show();

            Platform.runLater(() ->  {
                timeEnd = System.currentTimeMillis();
                System.out.println("Layout Time: " + (timeEnd - timeStart) + "ms");
            });
        }).run();
    }

    private <T> void generateCellFactory(TableColumn<T, String> column) {
        column.setCellFactory(cell -> {
            return new TableCell<T, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    setText("Test");
                }

            };
        });
    }
}

在我的电脑上的表现如下:

总列数:780 布局时间:9810毫秒

总列数:3905 布局时间:43920ms

有什么办法可以改善表现吗?或者可以在TableColumn上使用任何分页?

0 个答案:

没有答案