javaFX:在任务中打开第二个阶段,等待它关闭,然后继续第一阶段。
问题描述:
在tableView eventHandler上,当用户检测到F1时,它会打开一个带有comboBox和两个按钮的第二个阶段。如果用户从组合框中选择了一个条目并且击中了第一个按钮,那么它将所选var的值写入文件,关闭第二个阶段并返回到第一个阶段,在那里打开上面的文件进行读取,获取变量并将其存储在用户按下F1时激活的表格行的第一列中。部分:阶段,读/写文件,更新tableview所有工作正常,但当更改代码,我尝试应用服务(任务)第二阶段不显示。换句话说,两个阶段是不合适的。javaFX:在任务中打开第二个阶段,等待它关闭,然后继续第一阶段。
// INSIDE mainContoller
table.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent t) {
TablePosition firstCell = table.getSelectionModel().getSelectedCells().get(0);
//Integer col = firstCell.getColumn();
//Integer row = firstCell.getRow();
col = firstCell.getColumn();
row = firstCell.getRow();
//System.out.println(firstCell.getColumn());
if (t.getCode() == KeyCode.F1){
System.out.println("F1 pressed");
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
//Do Long running work here<<<
System.out.println("In task 1");
Parent root1 = null;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Customer.fxml"));
try {
root1 = (Parent) fxmlLoader.load();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
stage.setTitle("ABC");
System.out.println("In task 2");
stage.setScene(new Scene(root1));
stage.show();
System.out.println("In task 3");
return null;
}
};
}
@Override
protected void succeeded() {
//Called when finished without exception
System.out.println("OnSucceeded");
TextFileReadWrite getCustomer = new TextFileReadWrite();
String cusName = "";
try {
cusName = getCustomer.readFromFile("C:\\gasoum\\YannaKSIA\\ForitiTimologisi\\tempFiles\\tempCustomer.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Col:" + col + " Row: "+ row);
checkCusFile = true;
oList.get(row).cusEponymia = cusName;
table.refresh();
}
};
service.start(); // starts Thread
}
}
});
// CustomerContreller.java file
package application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
public class CustomerController extends Thread{
public String customerName;
@FXML Button customerChoseButton;
@FXML Button customerExitButton;
@FXML ComboBox cusComboBox ;
public String cus;
public void initialize() {
cus = "Testing";
cusComboBox.getItems().addAll(
"Customer 1",
"Customer 2",`
"Customer 3"
);
//AUTO COMPLETE COMBOBOX
new AutoCompleteComboBoxListener<>(cusComboBox);
customerChoseButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// WRITE SELECTED CUSTOMER INTO FILE
TextFileReadWrite wC = new TextFileReadWrite();
String selectedCus = (String) cusComboBox.getSelectionModel().getSelectedItem().toString();
wC.writeToFile(selectedCus, "C:\\gasoum\\YannaKSIA\\ForitiTimologisi\\tempFiles\\tempCustomer.txt");
customerChoseButton.getScene().getWindow().hide();
}
});
customerExitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
customerExitButton.getScene().getWindow().hide();
}
});
}
}
// TextFileReadWrite.java
package application;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TextFileReadWrite {
public TextFileReadWrite(){
}
public void writeToFile(String xcontent, String xfname){
try {
File file = new File(xfname);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(xcontent);
bw.close();
//System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
public String readFromFile(String xfname) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(xfname));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
return everything;
} finally {
br.close();
}
}
}
答
你有几件事情错在你的代码:
- 你正在创建并显示在后台线程
Stage
。这违反了线程政策,并会抛出IllegalStateException
(请参阅documentation)。因此舞台将不会显示,并且您的方法因异常而终止,因此不会调用onSucceeded
处理程序。 - 如果
call()
方法确实成功,它将基本完成并立即终止。拨打stage.show()
会显示舞台,然后返回。因此,如果它成功,那么在启动服务之后(并且在用户有机会与舞台进行交互之前),将会或多或少地调用onSucceeded
处理程序。
它看起来并不像你从舞台上得到任何信息,所以在这里真的不需要任何线程。只需在当前(即FX应用程序)线程中直接显示舞台。如果您需要等待用户输入,请使用stage.showAndWait()
而不是stage.show()
。
+0
stage.showAndWait()是我所需要的。感谢您的支持James_D。 – Gasoum
从您的问题标题看,问题看起来类似于:[我可以暂停后台任务/服务?](http://*.com/questions/14941084/javafx2-can-i-pause-a-background-任务服务),但是从问题文本和代码(我不是很了解并且由于众多的正切原因而显得不正确),我真的不知道这是否是你想要做的。 – jewelsea