在tableView eventHandler上,当用户检测到F1时,它会打开第二个阶段,其中包含一个comboBox和两个按钮。如果用户从comboBox中选择一个条目并点击第一个按钮,则它将所选var的值写入文件,关闭第二个阶段并返回第一个阶段,在那里打开上述文件进行读取,获取变量从文件中将其存储在用户按下F1时激活的表格行的最右列中。以下部分:阶段,读/写文件,更新tableview都可以正常工作,但是当更改代码并尝试应用服务(任务)时,第二阶段不会显示。换句话说,两个阶段不协调。
// 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();
}
}
}
答案 0 :(得分:1)
您的代码中有几个问题:
Stage
。这违反了线程策略,并会抛出IllegalStateException
(请参阅documentation)。因此,该阶段不会显示,并且您的call()
方法会以异常终止,因此onSucceeded
处理程序不会被调用。call()
方法确实成功,它将基本完成并立即终止。调用stage.show()
会显示该阶段,然后返回。因此,如果它成功了,那么在您启动服务之后(以及在用户有机会与舞台交互之前),或多或少会立即调用onSucceeded
处理程序。看起来你从你所展示的舞台上得到的任何信息都没有,所以这里真的不需要任何线程。只需在当前(即FX应用程序)线程中直接显示阶段。如果您需要等待用户输入,请使用stage.showAndWait()
代替stage.show()
。