3

I am creating a javaFX application with a button and a progress bar. In which i can press a button, and 40 http requests get scent, and the progress bar should be updated through the process, using updateProgress() method. Now problem is the app freezes once i press the start button, and while the requests are being sent, and it unfreezes when the process is done.

My two questions here are:

1- What am i doing wrong, and how can i avoid the freezing?, or what is the simplest way to make the user able to interact with the UI while a task is being performed in the backgroud?

2- Is my way of updating the progressBar through the process good, or is there a better standard way?

.

Edit: the question this question was a possible duplicate of didn't hit the main spot of my question, and seems rather complicated. I got the part where it says i can't run background tasks on the EDT, but what can i do about it?

Controller class

public class myApp implements Initializable {
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        for (int i = 0; i < 40; i++) {
            signed_requests.add("fjiwejf939qf");
        }
    }

    @FXML
    Button btnLuckySpin;
    @FXML
    ProgressBar progressBar;

    ArrayList<String> signed_requests = new ArrayList<>(100);

    public void handleMouseClicks(MouseEvent event) {
        Node source = (Node) event.getSource();
        if (source == btnLuckySpin) {
            runLuckySpin();
        }
    }

    private void runLuckySpin() {
        int counter = 1;

        progress_steps = 4 * signed_requests.size();

        for (String signed_request : signed_requests) {
            setProgress();
            LuckySpin luckySpin = new LuckySpin(signed_request);
            setProgress();
            luckySpin.run();
            setProgress();
            counter++;
            setProgress();
        }
        progress_steps = 0;
    }

    private int progress_steps = 0;

    private void setProgress() {
        double progress = 0;
        progress += 100 / progress_steps;
        progressBar.setProgress(progress);
    }
}
Rafat Mahmoud
  • 130
  • 3
  • 16

0 Answers0