0

I have a button that when pressed should open a new scene from an fxml file:

@FXML
private void button(ActionEvent event) throws IOException {

    navigation.getChildren().clear();
    AnchorPane newPane = FXMLLoader.load(getClass().getResource("Controller.fxml"));
    this.navigation.getChildren().setAll(newPane);

}

but what's happening is that the code within the controller of the fxml file is performed first before the scene is actually opened.

So in the controller class I have:

public void initialize(URL url, ResourceBundle rb) {

    try {             

        method1();

        Thread.sleep(2000);

        method2();

        Thread.sleep(2000);

        method3();

    } catch (InterruptedException ex) {
        Logger.getLogger(ScanningController.class.getName()).log(Level.SEVERE, null, ex);
    }


} 

public void method1(){

    System.out.println("method1");

    progressBar.setProgress(0.33);

}

public void method2(){

     System.out.println("method2");

    progressBar.setProgress(0.66);

}

public void method3(){

     System.out.println("method3");

    progressBar.setProgress(1);

}

method1, method2, method3 are all performed THEN the scene is opened but I want these methods to happen while the new scene is open.

I don't know why it does this.

Any suggestions would be appreciated. Thank you.

Edit: This is just one example, it's not necessarily just for the progress bar, it's happening with all my controller classes. For example, I have a class that sends and receives bytes to an Arduino from a Raspberry Pi and the scene opens straight after the code as been implemented.

Doja
  • 135
  • 1
  • 2
  • 10
  • Are `method1()` and others defined inside `intialize()` of the controller class? – ItachiUchiha Feb 26 '15 at 20:20
  • Don't sleep the JavaFX application thread. What are method1,2,3 doing that you want then them to do something after the scene is displayed? I guess, I'm asking why are you trying to do this and what are your trying to accomplish? – jewelsea Feb 26 '15 at 20:23
  • @ItachiUchiha yes they are. – Doja Feb 26 '15 at 20:24
  • @jewelsea I'm basically trying to change the value of the progress bar every 2 seconds. By the time the new scene opens, the progress bar is already full because all the actions are performed beforehand. – Doja Feb 26 '15 at 20:28
  • @jewelsea it's not necessarily the progress bar I want to fix, it's everywhere this happens. – Doja Feb 26 '15 at 20:48
  • But the general solution is the same as the specific solution for a progress bar . . . you use the [JavaFX concurrency utilities](http://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm). You should not be encountering such issues everywhere in a program, but only where you are attempting to perform long running or blocking tasks on the JavaFX application thread. If you really don't think the issue is the same, you can post a new question, which explains your issue in more detail and why the existing questions and solutions do not fit your scenario. – jewelsea Feb 26 '15 at 20:59

0 Answers0