运行应用程序时更新JavaFX控件
我正在使用JavaFx创建一个Java独立应用程序。 我已经看到了一些示例,但我无法理解如何在我的代码场景中使用javaFX运行应用程序时更新JavaFX控件,java,javafx,javafx-2,javafx-8,Java,Javafx,Javafx 2,Javafx 8,我正在使用JavaFx创建一个Java独立应用程序。 我已经看到了一些示例,但我无法理解如何在我的代码场景中使用javaFX任务 这是我正在调用的控制器函数,用于从SceneBuilder设置的按钮操作--> 现在,随着代码一步一步地进行,我正在逐步更新同一函数中的多个控件,但它最终会在执行完成时得到更新 我想在执行时更新控件,如代码所示 这可能是其他问题的重复: 也许还有其他一些问题 作为一种总体方法,您定义了一个任务,然后在任务的执行体中,使用Platform.runLater
任务
这是我正在调用的控制器函数,用于从SceneBuilder设置的按钮操作-->
现在,随着代码一步一步地进行,我正在逐步更新同一函数中的多个控件,但它最终会在执行完成时得到更新
我想在执行时更新控件,如代码所示 这可能是其他问题的重复:
也许还有其他一些问题
作为一种总体方法,您定义了一个任务,然后在任务的执行体中,使用Platform.runLater()、updateProgress()和其他机制来实现所需的功能。有关这些机制的进一步解释,请参见相关问题
final ImageView imageViewObj = new ImageView();
Task<Void> task = new Task<Void>() {
@Override protected Void call() throws Exception {
//Some Operations are carried out
//..
//Then I want to set Image in ImageView
// use Platform.runLater()
Platform.runLater(() -> imageViewObj.setImage(myImage));
// Some Code Here
//..
// Set Progress
updateProgress(0.1, 1);
// Some Code Here
//..
// Set Progress
updateProgress(0.2, 1);
int variable = 2;
final int immutable = variable;
// Maybe change some other Controls
// run whatever block that updates the controls within a Platform.runLater block.
Platform.runLater(() -> {
// execute the control update logic here...
// be careful of updating control state based upon mutable data in the task thread.
// instead only use immutable data within the runLater block (avoids race conditions).
});
variable++;
// some more logic related to the changing variable.
return null;
}
};
ProgressBar updProg = new ProgressBar();
updProg.progressProperty().bind(task.progressProperty());
Thread thread = new Thread(task, "my-important-stuff-thread");
thread.setDaemon(true);
thread.start();
final-ImageView-imageViewObj=new-ImageView();
任务=新任务(){
@Override protected Void call()引发异常{
//进行了一些操作
//..
//然后我想在ImageView中设置图像
//使用Platform.runLater()
Platform.runLater(()->imageViewObj.setImage(myImage));
//这里有一些代码
//..
//设定进度
updateProgress(0.1,1);
//这里有一些代码
//..
//设定进度
updateProgress(0.2,1);
int变量=2;
final int immutable=变量;
//也许可以改变一些其他的控制
//运行更新Platform.runLater块中控件的任何块。
Platform.runLater(()->{
//在此处执行控制更新逻辑。。。
//注意根据任务线程中的可变数据更新控件状态。
//相反,只在runLater块中使用不可变数据(避免争用条件)。
});
变量++;
//与变化变量相关的更多逻辑。
返回null;
}
};
ProgressBar updProg=新的ProgressBar();
updplog.progressProperty().bind(task.progressProperty());
线程线程=新线程(任务,“我的重要东西线程”);
setDaemon(true);
thread.start();
这可能是其他问题的重复:
也许还有其他一些问题
作为一种总体方法,您定义了一个任务,然后在任务的执行体中,使用Platform.runLater()、updateProgress()和其他机制来实现所需的功能。有关这些机制的进一步解释,请参见相关问题
final ImageView imageViewObj = new ImageView();
Task<Void> task = new Task<Void>() {
@Override protected Void call() throws Exception {
//Some Operations are carried out
//..
//Then I want to set Image in ImageView
// use Platform.runLater()
Platform.runLater(() -> imageViewObj.setImage(myImage));
// Some Code Here
//..
// Set Progress
updateProgress(0.1, 1);
// Some Code Here
//..
// Set Progress
updateProgress(0.2, 1);
int variable = 2;
final int immutable = variable;
// Maybe change some other Controls
// run whatever block that updates the controls within a Platform.runLater block.
Platform.runLater(() -> {
// execute the control update logic here...
// be careful of updating control state based upon mutable data in the task thread.
// instead only use immutable data within the runLater block (avoids race conditions).
});
variable++;
// some more logic related to the changing variable.
return null;
}
};
ProgressBar updProg = new ProgressBar();
updProg.progressProperty().bind(task.progressProperty());
Thread thread = new Thread(task, "my-important-stuff-thread");
thread.setDaemon(true);
thread.start();
final-ImageView-imageViewObj=new-ImageView();
任务=新任务(){
@Override protected Void call()引发异常{
//进行了一些操作
//..
//然后我想在ImageView中设置图像
//使用Platform.runLater()
Platform.runLater(()->imageViewObj.setImage(myImage));
//这里有一些代码
//..
//设定进度
updateProgress(0.1,1);
//这里有一些代码
//..
//设定进度
updateProgress(0.2,1);
int变量=2;
final int immutable=变量;
//也许可以改变一些其他的控制
//运行更新Platform.runLater块中控件的任何块。
Platform.runLater(()->{
//在此处执行控制更新逻辑。。。
//注意根据任务线程中的可变数据更新控件状态。
//相反,只在runLater块中使用不可变数据(避免争用条件)。
});
变量++;
//与变化变量相关的更多逻辑。
返回null;
}
};
ProgressBar updProg=新的ProgressBar();
updplog.progressProperty().bind(task.progressProperty());
线程线程=新线程(任务,“我的重要东西线程”);
setDaemon(true);
thread.start();
谢谢,我通过这个例子理解了它。现在有了更清晰的想法。谢谢,我通过这个例子理解了它。现在有了更清晰的想法。