gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:21:52 28 4
gpt4 key购买 nike

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

**我尝试创建硬编码数据并用它们填充 GridPane 只是为了测试,但同样花费了太长时间,因此我得出结论,导致按钮需要很长时间才能显示的问题是填充 GridPane,不从数据库加载数据!

有什么办法可以更快地填充GridPane吗?我使用 2 个 for 循环,一个用于行,另一个用于列,因此复杂度为(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);
}

最佳答案

其中存在一些潜在的性能 killer 。

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

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

您能提供一些数字吗?对你来说什么是慢的?

关于JavaFX 使用线程更快地动态填充 GridPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149717/

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