2013-11-20 23 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

相关问题