问题
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
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/52968067/javafx-frozen-gui-when-adding-button-on-flow-panel