• Как вернуть значение в основной поток и отобразить его?

    @nuclearthinking Автор вопроса
    Почти сразу дошло до меня решение
    может кому пригодится в будущем

    нужно последнюю строку обернуть в Platform.runLater();

    работающий метод будет выглядеть вот так

    protected void calculationStart() {
            double progress = 0.0;
            String s1 = steamIdTextArea.getText();
            int accountId = Integer.parseInt(s1);
            MatchHistory history = null;
            try {
                history = api.getMatchHistory(MATCHES_AMOUNT, accountId);
            } catch (RuntimeException e) {
                System.out.println(e.getMessage());
                System.out.println("Task can't be completed");
                throw new RuntimeException("Closing ...");
            } catch (IOException e) {
                e.printStackTrace();
            }
            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);
            Platform.runLater(() -> resultText.setText("Average KDA for last " + MATCHES_AMOUNT + " rating games = " + averageKDA));
        }
    Ответ написан
    Комментировать
  • Как читать с консольного окна, если оно не выводит ничего в stdout?

    @nuclearthinking
    Ковыряясь в одном интересном приложении натыкался на что то что возможно вам подойдёт

    метод выполняет консольную команду
    public static int runProcess(File workDir, String... command) throws Exception {
            ProcessBuilder pb = new ProcessBuilder(command);
            pb.directory(workDir);
            pb.environment().put("JAVA_HOME", System.getProperty("java.home"));
            if(!pb.environment().containsKey("MAVEN_OPTS")) {
                pb.environment().put("MAVEN_OPTS", "-Xmx1024M");
            }
    
            Process ps = pb.start();
            (new Thread(new Builder.StreamRedirector(ps.getInputStream(), System.out))).start();
            (new Thread(new Builder.StreamRedirector(ps.getErrorStream(), System.err))).start();
            int status = ps.waitFor();
            if(status != 0) {
                throw new RuntimeException("Error running command, return status !=0: " + Arrays.toString(command));
            } else {
                return status;
            }
        }


    метод StreamRedirector использующийся в предыдущем методе
    private static class StreamRedirector implements Runnable {
            private final InputStream in;
            private final PrintStream out;
    
            public void run() {
                BufferedReader br = new BufferedReader(new InputStreamReader(this.in));
    
                try {
                    String ex;
                    while((ex = br.readLine()) != null) {
                        this.out.println(ex);
                    }
    
                } catch (IOException var3) {
                    throw Throwables.propagate(var3);
                }
            }
    
            @ConstructorProperties({"in", "out"})
            public StreamRedirector(InputStream in, PrintStream out) {
                this.in = in;
                this.out = out;
            }
        }


    попробуйте исследовать библиотеки которые используются данным методом
    боюсь что готовое решение я вам предоставить не смогу, ещё очень слабо разбираюсь в Java
    Ответ написан
    Комментировать