1

In many samples it is shown how to extend Application method to have JavaFX app composed and ran.

But what if I don't want to? What if I want to configure app dynamically from my code? Example is below:

import javafx.application.Application;
import javafx.stage.Stage;

public class HollowTry {

   public static class HollowApplication extends Application {
      @Override
      public void start(Stage primaryStage) throws Exception {
      }
   }

   public static void main(String[] args) {
      Application.launch(HollowApplication.class, args);

      // now I want to set title, scene etc... how?

   }

}

Please don't dispute on why I need it.

UPDATE

Okay, launch() is never terminated, I didn't check this. Anyway, I need to have a way to build application programmatically, without any hardcoding.

UPDATE 2

I was wishing con build application from Spring and I found the following solution for now.

JavaFX wrapper class

It wraps context initialization into FX thread and captures config classes to be accessible from start():

public class SpringJavaFX extends Application {

   private static Class<?>[] annotatedClasses;

   @Override
   public void start(Stage primaryStage) throws Exception {

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses);

      String title = (String) context.getBean("primaryStageTitle");
      primaryStage.setTitle(title);

      Scene scene = (Scene) context.getBean("primaryStageScene");
      primaryStage.setScene(scene);

      primaryStage.show();

   }

   public static void launch(Class<?>... annotatedClasses) {
      SpringJavaFX.annotatedClasses = annotatedClasses;
      Application.launch();

   }
}

Spring way building

And here is an example of spring-way building. Each component is a bean and created in place:

public class Attempt01_HelloWorld {

   @Configuration
   public static class Config {

      @Bean
      String primaryStageTitle() {
         return "Attempt01_HelloWorld";
      }

      @Bean
      Scene primaryStageScene() {
         Scene ans = new Scene(root(), 800, 600);
         return ans;
      }

      @Bean
      Button button() {
         Button ans = new Button();
         ans.setText("Say 'Hello World'");
         ans.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
               System.out.println("Hello World!");
            }
         });
         root().getChildren().add(ans);
         return ans;
      }

      @Bean
      StackPane root() {
         StackPane root = new StackPane();
         return root;
      }


   }

   public static void main(String[] args) {
      SpringJavaFX.launch(Config.class);
   }

}
Dims
  • 47,675
  • 117
  • 331
  • 600
  • The right approach is to configure the application in the start method. Launch() will not return until the application terminates. – Andreas Fester Apr 23 '16 at 06:15
  • 1
    Adding to @AndreasFester comment: *The launch method does not return until the application has exited, either via a call to Platform.exit or all of the application windows have been closed.* - So anything after `Application.launch(HollowApplication.class, args);` won't run until after the application has exited. – Jonny Henly Apr 23 '16 at 06:17
  • The `init` method is also a possibility. When `init` is called (or later), the exact same information as in the main method is available (`getParameters()` method). – fabian Apr 23 '16 at 08:24
  • 1
    Possible duplicate of [How to pass parameters to JavaFX application?](http://stackoverflow.com/questions/24611789/how-to-pass-parameters-to-javafx-application) – fabian Apr 23 '16 at 08:30
  • @AndreasFester I don't think this forced approach is "right". – Dims Apr 23 '16 at 09:56
  • @fabian no answer in "duplicate" question, of course I saw it. – Dims Apr 23 '16 at 09:57
  • 1
    I think it would be helpful if you highlighted what exactly you want to dynamically set. Do you just want to set up your view, like window width and height? Or what else do you want to be dynamic? – randers Apr 23 '16 at 09:59
  • Please post your solution as an actual answer instead of editing into the question body. – randers Apr 23 '16 at 10:37
  • Related (perhaps): http://stackoverflow.com/questions/32739199/javafx-software-design. Though I think you are misunderstanding the structure of a JavaFX application: you should think of the `start` method really as the replacement for the `main` method, so your `SpringJavaFX` is the correct approach. Also perhaps related is using Spring to manage controller and model classes: http://www.marshall.edu/genomicjava/2015/09/27/experiments-with-spring-and-javafx/ – James_D Apr 23 '16 at 15:04

1 Answers1

0

I'm not sure it would work, but you could try and add setter methods for the app's parameters inside the inner class, and try and call them from outside (e.g. from your main). Again, I don't know whether this would work or not, but I'd give it a try in your place.