I am new JFX and I am struggling with this. I am trying to run a simple simulation that has to print output (system.out and system.err) to textarea.
public class ConsoleController {
@FXML
public TextArea consoleLogscreen;
private class Console extends OutputStream {
@Override
public void write(int b) throws IOException {
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();
}
}
SimulationController:
public class SimulationController {
@FXML
private Button homebutton;
@FXML
private Button abortbutton;
private volatile Service<Void> backgroundThread;
private volatile Thread t;
private volatile Simulate sim;
@FXML
public void initialize() {
sim = new Simulate();
t = new Thread(sim);
backgroundThread = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
t.run();
return null;
}
};
}
};
backgroundThread.setOnCancelled(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
t.interrupt();
sim.shutDown(true);
}
});
backgroundThread.restart();
abortbutton.setDisable(false);
homebutton.setDisable(true);
}
@FXML
void onAbortClicked(ActionEvent event) throws IOException {
if (event.getSource() == abortbutton) {
backgroundThread.cancel();
}
}
@FXML
void onHomeClicked(ActionEvent event) throws IOException {
if (event.getSource() == homebutton) {
Utility.getHome((Stage) homebutton.getScene().getWindow());
}
}
}
Simulation.fxml:
<Pane prefHeight="500.0" prefWidth="750.0"xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.jfxabstract.view.SimulationController">
<children>
<Button fx:id="abortbutton" alignment="CENTER" contentDisplay="CENTER" layoutX="250.0" layoutY="430.0" mnemonicParsing="false" onAction="#onAbortClicked" styleClass="custombutton" text="Abort" textAlignment="CENTER" />
<Button fx:id="homebutton" alignment="CENTER" contentDisplay="CENTER" layoutX="450.0" layoutY="430.0" mnemonicParsing="false" onAction="#onHomeClicked" styleClass="custombutton" text="Home" textAlignment="CENTER" />
<fx:include source="Console.fxml" />
</children>
</Pane>
I am using javafx.concurrent.service to run my task but the application is freezing and also the printing the output is not in realtime.
Can anyone suggest where I might have gone wrong.
Thanks in advance.
Update:
the run method retrieves data from database and runs few validations by invoking other methods in same class. For simple abstract application I am using this
public void run() {
long i = 1;
while (i < 90) {
double k = Math.sqrt(Math.pow(i, 2) / Math.sqrt(i));
System.out.println("i: " + i + " Count: " + k);
if (!shutdown) {
break;
}
i++;
}
}
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)