2013-11-20 44 views
2

我正在使用JavaFx,並且我有一個EventHandler,當點擊一個按鈕時運行。處理程序應該1)在屏幕上放置一個進度條,並在後面創建一個新窗口。該窗口需要一段時間才能創建,所以進度條在那裏讓人們知道該程序不會凍結。如何從一個EventHandler內部單獨運行兩個進程?

這裏是包含事件處理程序代碼

公共HBox中SelectionSection(){ 最終HBox中HB =新HBox中();

GridPane grid = new GridPane(); 
    grid.setAlignment(Pos.BASELINE_LEFT); 




    hb.setStyle("-fx-background-color: linear-gradient(#CEF5FD 0%, #1864E2 100%);"); 

    hb.setPrefSize(350, HEIGHT); 



    Button done = new Button("Create Data"); 

    final CheckBox ch1 = new CheckBox("Class Stats"); 

    ch1.setSelected(true); 

    final CheckBox ch2 = new CheckBox("Class Chart"); 
    ch2.setSelected(true); 

    final CheckBox ch5 = new CheckBox("Interactive Stats"); 

    final CheckBox ch3 = new CheckBox("Objectives Summary"); 

    final CheckBox ch4 = new CheckBox("part 4"); 

    final JFXDragAndDrop JFX = this; 

    CheckBox sdf = new CheckBox(); 

    done.setOnAction(new EventHandler<ActionEvent>(){ 


     @Override 
     public void handle(ActionEvent arg0) { 

      if(dir!=null){ 
       grid.getChildren().add(p1); 
       new Excel(JFX, dir, ch1.isSelected(), ch2.isSelected(), ch3.isSelected(), ch4.isSelected(), ch5.isSelected()); 

      } 
      else{ 
       Platform.runLater(new Runnable(){ 
        public void run(){ 
         JFX.createError(2); 
        } 
       }); 
      } 

     } 
    }); 






GridPane.setConstraints(ch1, 7, 7, 1, 1,HPos.LEFT, VPos.CENTER); 
GridPane.setConstraints(ch2, 7, 8, 1, 1,HPos.LEFT, VPos.CENTER); 
GridPane.setConstraints(ch5, 7, 9, 1, 1, HPos.LEFT, VPos.CENTER); 
GridPane.setConstraints(ch3, 7, 10, 1, 1,HPos.LEFT, VPos.CENTER); 
GridPane.setConstraints(done, 7, 16, 1, 1,HPos.CENTER, VPos.CENTER); 
GridPane.setConstraints(p1,7,15,1,1,HPos.CENTER,VPos.CENTER); 

grid.getChildren().addAll(ch1,ch2,ch3,ch5,done);  
hb.getChildren().add(grid);  
    return hb; 
} 

下面是它應該是什麼樣子的圖片。點擊「完成」按鈕後,加載欄應該彈出。相反,它會在加載條彈出之前完成運行Excel()。

回答

0

試試這可能會幫助你... !!

初始化按鈕

一些變量

@FXML 
private ProgressBar bar; 
int max = 1000000; 
int i=0; 

現在執行事件

btn.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent t) 
     { 
      loadAppConfigurationFile(); 
     } 
    }); 

函數定義

private void loadAppConfigurationFile() { 
    Task task = new Task<Void>() { 
@Override public Void call() throws InterruptedException { 

    for (i=1; i<=max; i=i+10) { 
     if (isCancelled()) { 
      break; 
     } 
     // System.out.println("value of time - "+now.getMinutes()); 
     //if(i==1) 
     //{ 
      // Thread.sleep(pk); 
     //} 

        updateProgress(i, max); 


     //System.out.println("1"); 
        DecimalFormat df = new DecimalFormat("#.00"); 
     System.out.println("progress - "+df.format(bar.getProgress())); 
     Double abc = Double.parseDouble(df.format(bar.getProgress())); 

     if(abc==1.00) 
     { 
      System.out.println("at here"); 
      Parent root; 
       try 
       {      
     URL url = getClass().getResource("Check.fxml"); 
     FXMLLoader fxmlLoader = new FXMLLoader(); 
     fxmlLoader.setLocation(url); 
     fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); 
     root = (Parent)fxmlLoader.load(url.openStream());    
     Stage stage = new Stage(); 
     //sstage.initStyle(StageStyle.UNDECORATED); 
     // stage.setFullScreen(true); 
     stage.setTitle("Welcome User"); 
     stage.setScene(new Scene(root, 631, 437)); 

     stage.show(); 
       } 
       catch(IOException ea) 
       { 
        System.out.println(ea.toString()); 
        ea.printStackTrace(); 
       } 
     } 
    } 


    return null; 
} 
}; 

bar.progressProperty().bind(task.progressProperty()); 
new Thread(task).start(); 
} 
+0

謝謝您的回覆! 在這個'酒吧'的進展實際上不是基於窗口的進展,是嗎?它只是基於for循環? – user3013876

相關問題