Já vi muito código na plataforma sobre atualização do ProgressBar usando thread, mas tudo está relacionado a algum cálculo realizado ou atualização de alguma propriedade de controle. Não sei se é possível, mas o que preciso é que o ProgressBar mostre o progresso na criação de 225 (15 X 15) a 2500 (50 x 50) TextFields. Abaixo estão os arquivos e o .fxml. Quando incluí a parte do código do thread, a grade parou de aparecer. Agradeço desde já.
A aplicação
Aplicação de grade.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class GridCenterApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(GridCenterApplication.class.getResource("main-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Grid");
stage.setScene(scene);
stage.setMaximized(true);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Controle de grade.java
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import java.net.URL;
import java.util.ResourceBundle;
public class GridCenterController implements Initializable {
@FXML
private ScrollPane scpGrid;
@FXML
private Spinner<Integer> spnCols;
@FXML
private Spinner<Integer> spnRows;
@FXML
private ProgressBar pgbProgress;
GridPane gridPane;
private final int CELL_HORIZONTAL_GAP = 1;
private final int CELL_VERTICAL_GAP = 1;
private final int CELL_HORIZONTAL_SIZE = 40;
private final int CELL_VERTICAL_SIZE = 40;
private int totalCols = 0;
private int totalRows = 0;
@FXML
void onMnuItemNewGridAction(ActionEvent event) {
if(!(scpGrid.getContent() == null)){
scpGrid.setContent(null);
}
totalCols = spnCols.getValue();
totalRows = spnRows.getValue();
var newGrid = new Grid(totalCols, totalRows, CELL_HORIZONTAL_GAP, CELL_VERTICAL_GAP, CELL_HORIZONTAL_SIZE,
CELL_VERTICAL_SIZE, pgbProgress);
gridPane = newGrid.getGrid();
scpGrid.setContent(gridPane);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
SpinnerValueFactory<Integer> numberOfCols = new SpinnerValueFactory.IntegerSpinnerValueFactory(15, 50);
SpinnerValueFactory<Integer> numberOfRows = new SpinnerValueFactory.IntegerSpinnerValueFactory(15, 50);
spnCols.setValueFactory(numberOfCols);
spnRows.setValueFactory(numberOfRows);
scpGrid.contentProperty().addListener((observableValue, oldValue, newValue) -> {
if (newValue != null && newValue.isVisible()) {
pgbProgress.setProgress(0);
}
});
}
}
Grade.java
import javafx.concurrent.Task;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class Grid {
private final GridPane grid;
public Grid(int totalCols, int totalRows, int CELL_HORIZONTAL_GAP, int CELL_VERTICAL_GAP, int CELL_HORIZONTAL_SIZE,
int CELL_VERTICAL_SIZE, ProgressBar pgbProgress) {
grid = new GridPane();
grid.setHgap(CELL_HORIZONTAL_GAP);
grid.setVgap(CELL_VERTICAL_GAP);
TextField[][] arrayLetterField = new TextField[totalCols][totalRows];
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
pgbProgress.setProgress(0);
double total = totalCols * totalRows;
double i = 1.0;
for (int row = 0; row < totalRows; row++) {
for (int col = 0; col < totalCols; col++) {
arrayLetterField[col][row] = new TextField();
arrayLetterField[col][row].setMinSize(CELL_HORIZONTAL_SIZE, CELL_VERTICAL_SIZE);
arrayLetterField[col][row].setMaxSize(CELL_HORIZONTAL_SIZE, CELL_VERTICAL_SIZE );
grid.add(arrayLetterField[col][row], col, row);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
updateProgress(i, total);
i++;
}
}
return null;
}
};
pgbProgress.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
public GridPane getGrid() {
return grid;
}
}
visualização-principal.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<BorderPane prefHeight="767.0" prefWidth="1053.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.gridpanetest.GridCenterController">
<top>
<VBox prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<MenuBar fx:id="mnuBar" prefHeight="25.0" prefWidth="360.0">
<menus>
<Menu mnemonicParsing="false" text="Grid">
<items>
<MenuItem mnemonicParsing="false" onAction="#onMnuItemNewGridAction" text="New grid" />
</items>
</Menu>
</menus>
</MenuBar>
<Pane prefHeight="80.0" prefWidth="1053.0">
<children>
<Label layoutX="26.0" layoutY="15.0" text="Columns" />
<Label layoutX="26.0" layoutY="46.0" text="Rows" />
<Spinner fx:id="spnCols" layoutX="79.0" layoutY="11.0" prefHeight="25.0" prefWidth="57.0" />
<Spinner fx:id="spnRows" layoutX="79.0" layoutY="42.0" prefHeight="25.0" prefWidth="57.0" />
<ProgressBar fx:id="pgbProgress" focusTraversable="false" layoutX="185.0" layoutY="31.0" prefWidth="200.0" progress="0.0" />
</children>
</Pane>
</children>
</VBox>
</top>
<center>
<ScrollPane fx:id="scpGrid" style="-fx-background-color: #dbbb92; -fx-background: #dbbb92;" BorderPane.alignment="CENTER" />
</center>
</BorderPane>