Answer the question
In order to leave comments, you need to log in
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);
}
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);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question