0

I am puzzled by the following occurence within the basic javafx application start method: Why does the Button btn not appear on stage (when .show() is called) but only when the for loop has ended?

public class JavaFXApplication extends Application {

@Override
public void start(Stage stage) {
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction((ActionEvent ae) -> {
        System.out.println("Hello World!");
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    stage.setTitle("Hello World!");
    stage.setScene(scene);
    stage.show();
    for (int i = 0; i < 99999; i++) {
        System.out.println("Hello World # " + i);
    }

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

  • Use `Platform.runLater(() -> { for (int i = 0; i < 99999; i++) { System.out.println("Hello World # " + i); }}); `. see http://stackoverflow.com/questions/30249493/using-threads-to-make-database-requests – Kachna Nov 18 '15 at 20:02
  • 1
    @kachna That won't work. You need to execute the loop in a background thread, not schedule it to run on the FX Application Thread – James_D Nov 19 '15 at 01:13

0 Answers0