我有一个包含4个类的程序:
1) GUI.java -包含JavaFX进度指示器GUI的程序
2) mainRun.java -一个运行task1.java和task2.java的“主”类。在实际程序中,mainRun.java将负责整合task1.java和task2.java的一些信息,因此,该类是必需的。
3) task1.java -将在task2.java之前运行的程序
4) task2.java -将在task1.java之后运行的程序
我想基于task1.java和task2.java中的updateProgress(X,Y)函数更新GUI.java的进度指示器。
但是,mainRun.java将是调用task1.java的程序,然后是task2.java。因此,GUI.java的进度指示器将仅基于从mainRun.java调用的updateProgress(X,Y)函数来更新其进度。
何时在task1.java和task2.java中运行updateProgress(X,Y)函数(可能绕过mainRun.java?)时,如何更新GUI.java的进度指示器?
否则,是否有一种方法可以从task1.java和task2.java主动通知mainRun.java一个updateProgress(X,Y),并在每次通知时更新GUI.java的进度指示器?
这是我正在描述的问题的模拟伪示例:
GUI.java
public class GUI extends Application {
private mainRun mainTask;
@Override
public void start(Stage primaryStage) {
final ProgressIndicator progressIndicator = new ProgressIndicator(0);
final Button startButton = new Button("Start");
// Start Button.
startButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
mainTask = new mainRun();
progressIndicator.progressProperty().unbind();
progressIndicator.setProgress(0);
progressIndicator.progressProperty().unbind();
progressIndicator.progressProperty().bind(mainTask.progressProperty());
new Thread(mainTask).start();
}
});
... JavaFX stage code ...
}
}
mainRun.java
public class CopyTask extends Task<String> {
@Override
protected String call() throws Exception {
/** Update GUI.java's progress bar based on t1 here **/
task1 t1 = new task1();
ProgressIndicator progress = new ProgressIndicator(0);
progress.progressProperty().unbind();
progress.setProgress(0);
progress.progressProperty().bind(t1.progressProperty());
new Thread(t1).start();
t1.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, //
new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
/** Continue updating GUI.java's progress bar based on t2 here **/
task2 t2 = new task2();
progress.progressProperty().unbind();
progress.progressProperty().bind(t2.progressProperty());
new Thread(t2).start();
}
});
return "Done";
}
}
task1.java
public class task1 extends Task<String> {
protected String call() throws Exception {
for (int i =0; i<1000; i++) {
this.updateProgress(i,2000); // currently updates mainRun.java progress bar NOT GUI.java
}
return "";
}
}
task2.java
public class task2 extends Task<String> {
protected String call() throws Exception {
for (int i =1000; i<2000; i++) {
this.updateProgress(i,2000); // currently updates mainRun.java progress bar NOT GUI.java
}
return "";
}
更新: 通过用户c0der找到了解决方案:
progressIndicator.progressProperty()。bind(copyTask.t1.progressProperty()。add(copyTask.t2.progressProperty())); **
更新: 找到了这个thread,用一个可靠的例子完美地解决了所有问题。
答案 0 :(得分:1)
您可以通过将progressbar的progress属性绑定到两个进度属性来实现:
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TwoLongProcessWithOneProgressBar extends Application {
@Override
public void start(Stage stage) throws InterruptedException {
BorderPane mainPane = new BorderPane();
FlowPane topPane = new FlowPane(Orientation.HORIZONTAL, 5.,5.);
topPane.setPadding(new Insets(20.));
Panel panel = new Panel();
BorderPane.setAlignment(panel, Pos.CENTER);
Button button = new Button("Start");
button.setDefaultButton(true);
button.setOnAction(e -> {
panel.runLlongTask();
button.setDisable(true);
});
topPane.getChildren().add(button);
mainPane.setTop(topPane);
mainPane.setCenter(panel);
mainPane.setBottom(panel.getBar());
Scene scene = new Scene(mainPane, Color.WHITE);
stage.setScene(scene);
stage.centerOnScreen();
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class Panel extends HBox{
private final ProgressBar bar;
Panel(){
setPrefSize(150,50);
bar = new ProgressBar();
bar.prefWidthProperty().bind(widthProperty());
}
void runLlongTask() {
Task task1 = new Task1();
Thread th1 = new Thread(task1);
th1.setDaemon(true);
Task task2 = new Task2();
Thread th2 = new Thread(task2);
th2.setDaemon(true);
//bind progress bar to both task progress property
bar.progressProperty().bind(task1.progressProperty().add(task2.progressProperty()));
th1.start();
th2.start();
}
ProgressBar getBar() { return bar; }
}
class Task1 extends Task<Void>{
@Override
protected Void call() throws Exception {
for (int i =0; i<1000; i++) {
TimeUnit.MILLISECONDS.sleep(100);
updateProgress(i,2000);
}
return null;
}
}
class Task2 extends Task<Void>{
@Override
protected Void call() throws Exception {
for (int i =0; i<1000; i++) {
TimeUnit.MILLISECONDS.sleep(100);
updateProgress(i,2000);
}
return null;
}
}