N
N
nuclearthinking2015-09-24 19:49:24
Java
nuclearthinking, 2015-09-24 19:49:24

How to return a value to the main thread and display it?

Good day,
the problem is in the following,
I'm trying to fasten my small application with a progress bar
in the controller class, when the button is clicked, the following code is executed

public void calculate() {
        button.setDisable(true);
        new Thread(this::calculationStart).start();
        button.setDisable(false);
    }

new Thread(this::calculationStart).start();
here the following method is called in a separate thread
protected void calculationStart() {
        double progress = 0.0;
        String s1 = steamIdTextArea.getText();
        int accountId = Integer.parseInt(s1);
        MatchHistory history = null;
        try {
            try {
                history = api.getMatchHistory(MATCHES_AMOUNT, accountId);
            } catch (IOException e) {
                resultText.setText(e.getMessage());
            }
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
            System.out.println("Task can't be completed");
            throw new RuntimeException("Closing ...");
        }
        List<Long> matchesList = calc.matchIdList(history);
        ArrayList<MatchDetails> matchDetailses = new ArrayList<>();
        for (Long matchId : matchesList) {
            try {
                progress += 0.05;
                matchDetailses.add(api.getMatchDetails(matchId));
                final double finalProgress = progress;
                Platform.runLater(() -> progressBar.setProgress(finalProgress));
            } catch (IOException e) {
                progress += 0.05;
                final double finalProgress = progress;
                resultText.setText(e.getMessage());
                Platform.runLater(() -> progressBar.setProgress(finalProgress));
            }
        }
        List<int[]> scoreList = calc.playerScore(matchDetailses, accountId);
        double averageKDA = calc.averageKda(scoreList);
        resultText.setText("Average KDA for last " + MATCHES_AMOUNT + " rating games = " + averageKDA);
    }

I think it’s not entirely necessary to delve into the code, the problem is in the last line of this method
in it, I try to update the text from the main thread, but it doesn’t work by itself, hence the question, how can I update the text in the UI from the main thread after executing this method flow?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question