JavaFX Frozen GUI在流程面板上添加Button时 [英] JavaFX Frozen GUI When adding Button on flow panel

查看:384
本文介绍了JavaFX Frozen GUI在流程面板上添加Button时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JAVA FX


How to add 5000 buttons or labels to flow panel without freezing GUI in JAVA FX Just like this

Why I even need so many buttons well, I don't need that many but at least 500 - 1000. Coz I'm building an application Fonticon Tool

Slow is fine but not Freezing It's ok if the application is slow and take a couple of seconds to show all button But I don't want it to freeze progress bar and GUI

How it works I have an SQLite database with a couple of tables, each table has a list of values. An object gives me ArrayList values

What I'm looking for I'm looking for something like.

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)));

}

I also wanna be able to stop and restart Thread

What I tired I tried Thread, Task, Platform.runLater(update); but I'm not sure that I use them correctly

解决方案

Here an MCVE using 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);
    }

}

这篇关于JavaFX Frozen GUI在流程面板上添加Button时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆