How to update textarea(console)in realtime in JavaFX without loosing flow or freezing the application

蹲街弑〆低调 提交于 2019-11-29 15:31:09

With this example i made a code to get System.out.println and to update on real time

/**
 * 
 * @param str
 */
public void appendText(String str) {

    results.append(str);

    updated = true;
}

/**
 * 
 */
public void setupConsoleOutput() {

    backgroundThread = new Service<String>() {

        protected Task<String> createTask() {

            return new Task<String>() {

                protected String call() throws Exception {

                    while (true) {

                        if (isCancelled()) {

                            break;
                        }

                        if (updated) {

                            updateValue(results.toString());

                            updated = false;

                            Platform.runLater(() -> {

                                consoleOutput.setScrollTop(Double.MAX_VALUE);
                            });
                        }

                        if (exported) {

                            consoleOutput.setScrollTop(Double.MAX_VALUE);

                            exported = false;
                        }

                        Thread.sleep(100);
                    }

                    return results.toString();
                }
            };
        }
    };

    consoleOutput.textProperty().bind(backgroundThread.valueProperty());

    backgroundThread.start();

    backgroundThread.setOnCancelled(new EventHandler<WorkerStateEvent>() {

        public void handle(WorkerStateEvent event) {

        }
    });

    backgroundThread.restart();

    OutputStream out = new OutputStream() {

        @Override
        public void write(int b) throws IOException {

            appendText(String.valueOf((char) b));
        }
    };

    PrintStream ps = new PrintStream(out, true);

    System.setOut(ps);

    System.setErr(ps);
}

and work fine for me.

If you want do not use Platform.runLater() method, you can use the value property which represents the result of the task (service):

consoleLogscreen.textProperty().bind(backgroundThread.valueProperty());

Here is a complete example:

simulation.xml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane prefHeight="563.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SimulationController">
    <children>
        <ProgressBar fx:id="progressBar" layoutX="309.0" layoutY="14.0" prefHeight="18.0" prefWidth="162.0" progress="0.0" />
        <Button fx:id="abortbutton" alignment="CENTER" contentDisplay="CENTER" layoutX="250.0" layoutY="520.0" mnemonicParsing="false" onAction="#onAbortClicked" styleClass="custombutton" text="Abort" textAlignment="CENTER" />
        <Button fx:id="homebutton" alignment="CENTER" contentDisplay="CENTER" layoutX="450.0" layoutY="520.0" mnemonicParsing="false" onAction="#onHomeClicked" styleClass="custombutton" text="Home" textAlignment="CENTER" />
        <TextArea fx:id="consoleLogscreen" layoutX="15.0" layoutY="10.0" prefHeight="487.0" prefWidth="287.0" wrapText="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
        <Label fx:id="progress" layoutX="484.0" layoutY="15.0" text="Label" />

    </children>
</Pane>

SimulationController class

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.When;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;

/**
 * FXML Controller class
 *
 * @author 
 */
public class SimulationController implements Initializable {

    @FXML
    private Button abortbutton;
    @FXML
    private Button homebutton;

    private volatile Service<String> backgroundThread;
    @FXML
    private TextArea consoleLogscreen;
    @FXML
    private ProgressBar progressBar;
    @FXML
    private Label progress;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {

         backgroundThread = new Service<String>() {
            @Override
            protected Task<String> createTask() {
                return new Task<String>() {
                    StringBuilder results = new StringBuilder();
                    @Override
                    protected String call() throws Exception {
                        long i = 1;
                        String s = null;
                        while (i < 90) {
                            if(isCancelled()){
                                break;
                            }
                            double k = Math.sqrt(Math.pow(i, 2) / Math.sqrt(i));
                            results.append("i: ").append(i).append(" Count: ").append(k).append("\n");
                            updateValue(results.toString());
                            updateProgress((100*i)/90, 90);
                            Thread.sleep(100);
                            i++;
                        }

                        return results.toString();
                    }
                };
            }
        };
        consoleLogscreen.textProperty().bind(backgroundThread.valueProperty());
        progressBar.progressProperty().bind(backgroundThread.progressProperty());
        progress.textProperty().bind(new When(backgroundThread.progressProperty().isEqualTo(-1)).then("Unknown")
                .otherwise(backgroundThread.progressProperty().multiply(100).asString("%.2f%%")));
        backgroundThread.start();
        backgroundThread.setOnCancelled(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent event) {

            }
        });
        backgroundThread.restart();
        abortbutton.setDisable(false);
        homebutton.setDisable(true);
    }

    @FXML
    private void onAbortClicked(ActionEvent event) {
        if (event.getSource() == abortbutton) {
            backgroundThread.cancel();
        }
    }

    @FXML
    private void onHomeClicked(ActionEvent event) {
    }

}

As a strict rule, all nodes in a scenegraph must be accessed on the fx-application thread. To guarantee that rule in your context, you can wrap the appendText into Platform.runlater(), something like:

public class ConsoleController {
    @FXML
    public TextArea consoleLogscreen;

    private class Console extends OutputStream {

        @Override
        public void write(int b) throws IOException {
            Platform.runLater(() ->
               consoleLogscreen.appendText(String.valueOf((char) b));
            }
        }
    }

    @FXML
    public void initialize() throws IOException {
        Console console = new Console();
        PrintStream ps = new PrintStream(console, true);
        System.setOut(ps);
        System.setErr(ps);
        System.err.flush();
        System.out.flush();
    }
}

(untested, just copied and adjusted your code)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!