首页 > 解决方案 > JavaFX Frozen GUI 在流面板上添加按钮时

问题描述

如何在不冻结 JAVA FX 中的 GUI 的情况下向流程面板添加 5000 个按钮或标签就像这样

为什么我什至需要这么多按钮 ,我不需要那么多,但至少需要 500 - 1000 个。因为我正在构建一个应用程序字体图标工具

慢是好的但不是冻结 如果应用程序很慢并且需要几秒钟来显示所有按钮也没关系但我不希望它冻结进度条和GUI

它是如何工作 的 我有一个带有几个表的 SQLite数据库,每个表都有一个列表。一个对象给了我 ArrayList 值

我正在寻找什么我正在寻找 类似的东西。

FlowPane fp = new FlowPane(); 

for(String fonticon_code : DatabaseTable.getlist()) //getlist() returns an array list of Strings
{
  fp.getChildren.add(new button().setGraphic(new FontIcon(fonticon_code)));

}

我也希望能够停止并重新启动线程

我累了 我尝试了 Thread, Task, Platform.runLater(update); 但我不确定我是否正确使用它们

标签: javajavafxconcurrency

解决方案


这里是一个MCVE,使用.ControlsFX GridView

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.controlsfx.control.GridCell;
import org.controlsfx.control.GridView;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication287 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        ObservableList observableList = FXCollections.observableArrayList(Font.getFamilies());
        GridView<String> myGrid = new GridView(observableList);
        myGrid.setHorizontalCellSpacing(0);
        myGrid.setVerticalCellSpacing(0);
        myGrid.setCellFactory(gridView -> {
            return new GridCell<String>()
            {
                Button button = new Button("ABC");

                {
                    button.setPrefWidth(60);
                    button.setPrefHeight(60);
                }

                @Override
                public void updateItem(String item, boolean empty)
                {
                    if (empty || item == null) {
                        setText(null);
                        setGraphic(null);
                    }
                    else {
                        button.setFont(new Font(item, 14));
                        setGraphic(button);
                    }

                }
            };
        });

        StackPane root = new StackPane(myGrid);
        Scene scene = new Scene(root, 500, 700);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

在此处输入图像描述


推荐阅读