2016-06-08 98 views
0

在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(); 
     } 


    } 


} 
+0

從您的問題標題看,問題看起來類似於:[我可以暫停後臺任務/服務?](http://stackoverflow.com/questions/14941084/javafx2-can-i-pause-a-background-任務服務),但是從問題文本和代碼(我不是很瞭解並且由於衆多的正切原因而顯得不正確),我真的不知道這是否是你想要做的。 – jewelsea

回答

1

你有幾件事情錯在你的代碼:

  1. 你正在創建並顯示在後臺線程Stage。這違反了線程政策,並會拋出IllegalStateException(請參閱documentation)。因此舞臺將不會顯示,並且您的方法因異常而終止,因此不會調用onSucceeded處理程序。
  2. 如果call()方法確實成功,它將基本完成並立即終止。撥打stage.show()會顯示舞臺,然後返回。因此,如果它成功,那麼在啓動服務之後(並且在用戶有機會與舞臺進行交互之前),將會或多或少地調用onSucceeded處理程序。

它看起來並不像你從舞臺上得到任何信息,所以在這裏真的不需要任何線程。只需在當前(即FX應用程序)線程中直接顯示舞臺。如果您需要等待用戶輸入,請使用stage.showAndWait()而不是stage.show()

+0

stage.showAndWait()是我所需要的。感謝您的支持James_D。 – Gasoum

相關問題