几天来,我一直在尝试更新Javafx用户界面,同时自动发送2封或更多电子邮件。UI应该用“连接”、“发送”和“发送”更新标签。我读过关于Runnable、任务、TimeLine的文章,但我不太明白我应该使用哪种方法,以及它是如何工作的。
每当我读到关于Platform.runLater()的文章时,我都会使用它,但是JavaFX Gui中的动画会结冰,标签只会在发送所有电子邮件时发生变化。
我将很快用我的代码更新这篇文章,但是有人能告诉我我应该使用什么样的线程,以及如何轻松地更新ui吗?
//编辑
感谢您的帮助,Branislav,但现在我得到了“不在FX应用程序线程”-Error。下面的代码不是我的“扩展应用程序”-class,请看一下。
public class ControllerOCM implements Initializable{
@FXML
private ComboBox<String> comboEmpf;
@FXML
private Label lblStatus;
@FXML
private Label lblStatus1;
@Override
public void initialize(URL location, ResourceBundle resources) {
hint.setText("some hint");
}
public void sende(ActionEvent event) throws Exception {
switch (comboEmpf.getValue()) {
case "A": System.out.println("A");break;
case "B":
File fTEST = new File("C:\\B\\");
File[] fpathTEST = fTEST.listFiles();
String[] fnameTEST = fTEST.list();
for (int i=0; i<fpathTEST.length; i++) {
SendenTask sendTEST = new SendenTask(mail@address.com,
"bodycontent",
"subject",
fpathTEST[i].toString(),
fnameTEST[i],
i, //count
fpathTEST.length); //max
new Thread(sendTEST).start();
}
break;
}
}
public class SendenTask extends Task<Void> {
private String adr;
private String body;
private String subj;
private String fp;
private String fn;
private int count;
private int max;
public SendenTask(String adr, String body, String subj, String fp, String fn, int count, int max) {
this.adr = adr;
this.body = body;
this.subj = subj;
this.fp = fp;
this.fn = fn;
this.count = count;
this.max = max;
}
@Override
protected Void call() throws Exception {
lblStatus1.setText("connecting");
//doing connectionAction
lblStatus1.setText("sending");
updateMessage("sending");
//doing sendingthingys
//sending complete
lblStatus1.setText("sent");
System.out.println("done");
updateMessage("done");
return null;
}
}
}//社论2
现在程序是一个又一个地发送电子邮件,但textproperty不能正常工作。它不是刷新文本,它只是清空标签。如果删除取消绑定行(在“成功”中),则只运行一次发送任务。在此之后,对于所有其他发送任务,它再次为空。
public class ControllerOCM implements Initializable{
@FXML
private ComboBox<String> comboEmpf;
@FXML
private Label lblStatus;
@FXML
private Label lblStatus1;
@Override
public void initialize(URL location, ResourceBundle resources) {
hint.setText("some hint");
}
public void sende(ActionEvent event) throws Exception {
switch (comboEmpf.getValue()) {
case "A": System.out.println("A");break;
case "B":
File fTEST = new File("C:\\B\\");
File[] fpathTEST = fTEST.listFiles();
String[] fnameTEST = fTEST.list();
for (int i=0; i<fpathTEST.length; i++) {
SendenTask sendTEST = new SendenTask(mail@address.com,
"bodycontent",
"subject",
fpathTEST[i].toString(),
fnameTEST[i],
i, //count
fpathTEST.length); //max
lblStatus1.textProperty().bind(sendTEST.messageProperty());
thread = new Thread(sendTEST);
thread.start();
}
break;
}
}
public class SendenTask extends Task<Void> {
private String adr;
private String body;
private String subj;
private String fp;
private String fn;
private int count;
private int max;
public SendenTask(String adr, String body, String subj, String fp, String fn, int count, int max) {
this.adr = adr;
this.body = body;
this.subj = subj;
this.fp = fp;
this.fn = fn;
this.count = count;
this.max = max;
}
@Override
protected Void call() throws Exception {
while(!sending) {
sending = true
updateMessage("connecting");
//doing connectionAction
updateMessage("sending");
//doing sendingthingys
//sending complete
updateMessage("done");
}
return null;
}
@Override
protected void succeeded() {
super.succeeded();
lblStatus1.textProperty().unbind();
sending = false;
}
}
}最大的问题是,lblStatus1.textProperty().bind(sendTEST.messageProperty());放在哪里,lblStatus1.textProperty().unbind();?在哪里?
发布于 2015-08-04 10:06:59
您必须使用Task类。
下面是一个小演示。让我们通过for循环来模拟耗时的任务:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class TaskDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
final Label label = new Label();
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
boolean fxApplicationThread = Platform.isFxApplicationThread();
System.out.println("Is call on FXApplicationThread: " + fxApplicationThread);
// Run your time consuming task here!
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
updateMessage("Time elapsed: " + i);
}
return null;
}
@Override
protected void succeeded() {
boolean fxApplicationThread = Platform.isFxApplicationThread();
System.out.println("Is call on FXApplicationThread: " + fxApplicationThread);
super.succeeded();
label.textProperty().unbind();
label.setText("Task done");
}
};
// Bind messageProperty of task for textProperty of Label
label.textProperty().bind(task.messageProperty());
stage.setScene(new Scene(label, 300, 200));
stage.show();
new Thread(task).start();
}
}事情很简单。您的耗时任务将冻结UI,直到它完成。通过使用Task类,您的耗时任务在后台线程上运行,从而使JavaFX线程响应。有时,您可以从call方法“发布”任务的结果。不更新后台线程上的JavaFX UI (在call方法中)。通过调用Platform.isFxApplicationThread(),您可以检查代码是否运行在FXApplicationThread上。如果您的代码没有在该线程上运行,请不要从那里更新您的JavaFX UI!
https://stackoverflow.com/questions/31806200
复制相似问题