JavaFX使用线程动态更快地填充GridPane

时间:2019-03-13 19:19:57

标签: java multithreading javafx java-threads

我正在从数据库中加载数据并在GridPane内部动态创建按钮!但是,由于有大量数据,因此花很长时间才能看到按钮! 我正在使用线程从数据库中加载数据,但是似乎创建每个按钮并填充GridPane花费的时间太长了!

**我尝试创建硬编码数据并仅将它们填充到GridPane中以进行测试,但这又花费了太长时间,因此我得出结论,导致按钮花费太长时间才能显示的问题是填满了GridPane,无法从数据库加载数据!

有什么方法可以更快地填充GridPane吗? 我正在使用2个循环,一个用于行,另一个用于列,所以复杂度是(n平方)!

我可以优化它吗?

这是我创建和填充GridPane的功能:

public void initialize() throws Exception {
    initializedProducts = new ArrayList<>();
    productsOnReceipt = new ArrayList<>();
    rows = new VBox();
    grandPrice = 0;

    //Initializing DatabaseConnections and accessors for each sections
    ProductDao burgerAccessor = new ProductDaoImpl();
    ProductDao burgerMenuAccessor = new ProductDaoImpl();
    ProductDao drinksAccessor = new ProductDaoImpl();

    Executor exec = Executors.newCachedThreadPool(runnable -> {
        Thread t = new Thread(runnable);
        t.setDaemon(true);
        return t;
    });

    initSections(burgers_section, 2, "PICI", exec, burgerAccessor);
    initSections(burgersMenu_section, 2, "TOPLI JADENJA", exec, burgerMenuAccessor);
    initSections(drinks_section, 1, "BEZALKOHOLNI PIJALOCI", exec, drinksAccessor);
}

产品初始化部分:

private void initSections(ScrollPane section, int rows, String category, Executor executor, ProductDao products) {
    GridPane grid = new GridPane();
    grid.setPadding(new Insets(5));
    grid.setHgap(20);
    grid.setVgap(-7);
    grid.setAlignment(Pos.TOP_LEFT);

    initProducts(grid, rows, category, executor, products);
    section.setContent(grid);
}

每个部分的产品初始化:

private void initProducts(GridPane grid, int rows, String category, Executor executor, ProductDao accessor) {
    Task<Products> productsResultTask = new Task<Products>() {
        @Override
        protected Products call() throws Exception {
            return accessor.getByCategory(category);
        }
    };

    productsResultTask.setOnFailed(e -> productsResultTask.getException().printStackTrace());

    productsResultTask.setOnSucceeded(e -> {
        if (productsResultTask.getValue() != null) {
            ArrayList<Product> products = productsResultTask.getValue().getProducts();
            int productCounter = 0;
            if (rows == 1) {
                for (int j = 0; j < products.size(); j++) {
                    initializeButton(category, products.get(productCounter), grid, 0, j);
                    initializedProducts.add(products.get(productCounter));
                    productCounter++;
                }
            } else {
                if (isEven(products.size())) {
                    for (int i = 0; i < rows; i++) {
                        for (int j = 0; j < products.size() / 2; j++) {
                            initializeButton(category, products.get(productCounter), grid, i, j);
                            initializedProducts.add(products.get(productCounter));
                            productCounter++;
                        }
                    }
                } else {
                    for (int i = 0; i < rows; i++) {
                        for (int j = 0; j < (products.size()-1) / 2; j++) {
                            initializeButton(category, products.get(productCounter), grid, i, j);
                            initializedProducts.add(products.get(productCounter));
                            productCounter++;
                        }
                    }
                    initializeButton(category, products.get(productCounter), grid, 0, ((products.size() - 1) / 2));
                    initializedProducts.add(products.get(productCounter));
                }
            }
        } else {
            System.out.println("Can't fetch products from DB!");
        }
    });
    executor.execute(productsResultTask);
}

**我认为下面的initializeButton()函数没有问题,但是以防万一,您可以看到工作的流程!

为每种产品初始化每个按钮:

private void initializeButton(String category, Product product, GridPane grid, int i, int j) {
        Label bName = new Label(product.getName());
        bName.setStyle("-fx-font-size: 16;");
        bName.setPrefWidth(130);
        bName.setWrapText(true);
        bName.setStyle("-fx-text-alignment: center;");
        bName.setAlignment(Pos.CENTER);

        Button button = new Button();
        button.setPrefHeight(110);
        button.setPrefWidth(130);

        BackgroundImage backgroundImage = new BackgroundImage(new Image(getClass().getResource("/images/burgerm.png").toExternalForm()),
                BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, new BackgroundSize(120, 100, false, false, false, false));
        Background background = new Background(backgroundImage);
        button.setBackground(background);
        button.setStyle("-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);");
        button.setOnAction(event -> {
            if (category.equals("TOPLI JADENJA")) {
                askForDrink(product);
            } else {
                addToReceipt(product);
            }

        });

        VBox item = new VBox();
        item.getChildren().addAll(button, bName);
        item.setAlignment(Pos.TOP_CENTER);
        item.setSpacing(-8);
        item.setPrefWidth(110);
        grid.add(item, j, i);
    }

1 个答案:

答案 0 :(得分:-2)

您那里有一些潜在的性能杀手。

为什么要一遍又一遍地加载backgroundImage。您可以在循环外将其加载一次并重复使用。

尝试将代码减少到绝对最小值。没有任何CSS和其他装饰的普通按钮,然后比较性能。

能否提供一些数字。您觉得什么很慢?