JavaFX-从线程实时更新线条图
在我的JavaFX应用程序中,我希望显示来自后台线程的实时数据。有人知道如何从后台线程更新折线图吗?非常感谢。下面是一些示例代码 商标 采样控制器JavaFX-从线程实时更新线条图,javafx,javafx-8,javafx-2,Javafx,Javafx 8,Javafx 2,在我的JavaFX应用程序中,我希望显示来自后台线程的实时数据。有人知道如何从后台线程更新折线图吗?非常感谢。下面是一些示例代码 商标 采样控制器 import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.chart.LineChart; import javafx.scene.control.Button; public class SampleController { @FXML
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.LineChart;
import javafx.scene.control.Button;
public class SampleController {
@FXML
Button btnStart;
@FXML
Button btnStop;
@FXML
LineChart myChart;
Process process;
@FXML
public void initialize() {
process = new Process();
}
public void start(ActionEvent event) {
process.start();
}
public void stop(ActionEvent event) {
process.stop();
}
}
进程类。启动线程
public class Process {
private Task task = new Task();
public void start(){
task.start();
}
public void stop(){
task.kill();
}
}
任务类。执行任务的线程类
public class Task extends Thread {
private boolean isActive = true;
@Override
public void run() {
while (isActive) {
try {
// Simulate heavy processing stuff
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Add a new number to the linechart
// Remove first number of linechart
}
}
public void kill(){
isActive = false;
}
}
主类
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/10.0.1"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.SampleController">
<children>
<Button fx:id="btnStart" mnemonicParsing="false" onAction="#start" text="Start" />
<LineChart fx:id="myChart">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<Button fx:id="btnStop" mnemonicParsing="false" onAction="#stop"
text="Stop" />
</children>
</VBox>
您需要从UI线程插入要在折线图中显示的值。因此,您可以简单地使用以下内容:
public class Task extends Thread {
private boolean isActive = true;
private LineChart<String, Number> chart;
private Series<String, Number> series = new Series<>();
public Task(LineChart<CategoryAxis, NumberAxis> chart) {
this.chart.getData().add(series);
}
@Override
public void run() {
while (isActive) {
try {
// Simulate heavy processing stuff
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(() -> {
// Add a new number to the linechart
this.series.getData().add(new Data<>("",0));
// Remove first number of linechart
this.series.getData().remove(0);
});
}
}
public void kill(){
isActive = false;
}
}
公共类任务扩展线程{
私有布尔值isActive=true;
专用线图;
专用系列=新系列();
公共任务(折线图){
this.chart.getData().add(系列);
}
@凌驾
公开募捐{
while(isActive){
试一试{
//模拟重加工物料
睡眠(10000);
}捕捉(中断异常e){
e、 printStackTrace();
}
Platform.runLater(()->{
//向折线图中添加新编号
this.series.getData().add(新数据(“,0));
//删除线条图的第一个编号
this.series.getData().remove(0);
});
}
}
公共空杀(){
isActive=假;
}
}
或者,如果您想在添加和删除值后执行某些操作,请使用以下方法:
public class Task extends Thread {
private boolean isActive = true;
private LineChart<String, Number> chart;
private Series<String, Number> series = new Series<>();
public Task(LineChart<CategoryAxis, NumberAxis> chart) {
this.chart.getData().add(series);
}
@Override
public void run() {
while (isActive) {
try {
// Simulate heavy processing stuff
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
FutureTask<Object> task = new FutureTask<Object>(new Callable<Object>() {
@Override
public Object call() throws Exception {
// Add a new number to the linechart
series.getData().add(new Data<>("",0));
// Remove first number of linechart
series.getData().remove(0);
return true;
}
});
Platform.runLater(task);
try {
System.out.println(task.get());
//TODO after insertion
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
public void kill(){
isActive = false;
}
}
公共类任务扩展线程{
私有布尔值isActive=true;
专用线图;
专用系列=新系列();
公共任务(折线图){
this.chart.getData().add(系列);
}
@凌驾
公开募捐{
while(isActive){
试一试{
//模拟重加工物料
睡眠(10000);
}捕捉(中断异常e){
e、 printStackTrace();
}
FutureTask task=新的FutureTask(新的可调用(){
@凌驾
公共对象调用()引发异常{
//向折线图中添加新编号
series.getData().add(新数据(“,0));
//删除线条图的第一个编号
series.getData().remove(0);
返回true;
}
});
Platform.runLater(任务);
试一试{
System.out.println(task.get());
//插入后的待办事项
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
}
公共空杀(){
isActive=假;
}
}