2
votes

I am using JavaFx, and I have an EventHandler that runs when a button is clicked. The Handler is supposed to 1) put a progress bar on the screen, and AFTERWARDS make a new window. The window takes a little while to create, so the progress bar is there to let people know that the program did not just freeze.

Here is the code that contains the EventHandler

public HBox SelectionSection(){ final 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;
}

Here is a picture of what it should look like. The loading bar should pop up as soon as you click the 'done' button. Instead, it finishes running Excel() before the loading bar pops up.

1

1 Answers

0
votes

try this it may help you...!!

initialize some variables

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

now perform event on button

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

function definition

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