如何从一个EventHandler内部分别运行两个进程?

时间:2013-11-20 17:39:09

标签: javafx

我正在使用JavaFx,并且我有一个在单击按钮时运行的EventHandler。处理程序应该1)在屏幕上放置一个进度条,然后AFTERWARDS打开一个新窗口。窗口需要一段时间才能创建,因此进度条可以让人们知道程序不仅仅是冻结。

以下是包含EventHandler

的代码

public HBox SelectionSection(){         最终HBox hb = new 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()。

1 个答案:

答案 0 :(得分: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();
}