JavaFX : invoking ' Application.launch(args) ' from a method other than main

11,037

You need to call the static method of the class which extends Application. You can call it from anywhere, not mandatory to call it from main( ) . Use the following :

OpenSite.launch(OpenSite.class);

For background knowledge on how JavaFX Application works, please go through Application JavaDoc. It is very well written and throws a lot of insight on how JavaFX Application is triggered.

You can also go through the following answer

Starting JavaFX from Main method of class which doesn't extend Application

Notes

  • The thread which calls Application.launch() and launches the primary Stage will not return unless the Stage is closed.
  • Make sure you make the call to launch() just once.
Share:
11,037

Related videos on Youtube

ReedWilliams19842004
Author by

ReedWilliams19842004

Updated on June 04, 2022

Comments

  • ReedWilliams19842004
    ReedWilliams19842004 almost 2 years

    Question

    Can I call ' Application.launch(args); ' from a method other than main? If so could you provide an example keeping in mind the below context?

    Background

    I'm build a learning/teaching, command/text application, that teaches the user about arrays. At the end of the main class, after the major application content has ran, I call ' ViewSiteOrExit.viewSitePromptPuSVM(); ', which gives the user the opposition to either: open the Oracle page on arrays, or exit the game.

    If the user wishes to view the Oracle page I invoke ' OpenSite.??????????(); ', which will open the page in an FX VBox. If not, exit.

    This is the first time I've ever used FX, and I'm tired, so any observations and suggestion, of and for, my code would really help, because I may be missing something.

    But my main question is how can/should I call ' OpenSite.??????????(); ', the method containing 'Application.launch(args);, if not from my main?

    If it must be called from main, how can I do so, only after the primary parts of the app has ran, and only if the user has input ' y '?

    Below is the .java that prompts the user to view the site or exit the game, and the .jave that opens the page.

    package mrArray;
    
    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.layout.VBox;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    
    public class OpenSite extends Application 
    {
        VBox vBoxOF = new VBox();
    
      public static void main(String[] args) 
      {
        Application.launch(args);
      }
    
      @Override
      public void start(Stage primaryStage) 
      {
        vBoxOF.setId("root");
    
        WebView  webViewBrowserOL = new WebView();
        WebEngine webEngineOL = webViewBrowserOL.getEngine();
        String urlSL = "http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html";
        webEngineOL.load(urlSL);
    
        vBoxOF.setPadding(new Insets(30, 50, 50, 50));
        vBoxOF.setSpacing(10);
        vBoxOF.setAlignment(Pos.CENTER);
        vBoxOF.getChildren().addAll(webViewBrowserOL);
    
        Scene sceneOL = new Scene(vBoxOF);
        primaryStage.setScene(sceneOL);
        primaryStage.show();
      }
    }
    

    Second Class

    package mrArray;
    
    
    public class ViewSiteOrExit 
    {
        /*
         * declare fields,
         */
        private static int statePrSIF;
        private static String enterYOrNPrSSOF;
    
        /*
         * declare method,
         * initialize field,
         * while, test(field) is passing execute,
         * switch, evaluates cases with value(field),
         * matching, execute statements,
         * 0, first case, prompt, y drop to if, reset value, use app again,
         * n drop to else, increment field, 1, second case,
         * invoke method to close app, reset field value to prevent double field invocation,
         * return flow to caller to prevent use of closing Scanner,
         */
         public static void viewSitePromptPuSVM() 
         {
             statePrSIF = 0;
             while (statePrSIF < 2) 
             {
                 switch (statePrSIF) 
                 {
                    case 0: 
                        System.out.println();
                        System.out.println("[:-)] One more question?");
                        System.out.println("Would you like to see what Oracle has to say about me?");
                        System.out.println("Enter ' y ' for yes.");
                        System.out.println("Enter ' n ' for no.");
                        break;
                    case 1:
                        goodByePuSVM();
                        statePrSIF = 0;
                        return;
                 }
    
                 enterYOrNPrSSOF = MrArray.scannerOF.next();
    
                 if(enterYOrNPrSSOF.equalsIgnoreCase("y")) 
                 {
                     statePrSIF = 0;
                     System.out.println("[:-)] Well bud, it's been fun.");
                     System.out.println("Here is that Orcale thing.");
                     System.out.println("See ya later!");
                     OpenSite.??????????();
                 }
                 else if(enterYOrNPrSSOF.equalsIgnoreCase("n")) 
                 {
                     statePrSIF++;
                 }  
             }
         }
    
         /*
          * declare method,
          * invoke methods, display output,
          * close Scanner, terminate,
          */
         public static void goodByePuSVM()
         {
                System.out.println("[:-)] Well bud, it's been fun.");
                System.out.println("See ya later!");
    
                MrArray.scannerOF.close();
         }
    }
    
  • ItachiUchiha
    ItachiUchiha over 9 years
    Yes, it will. Please go through my edit.
  • ReedWilliams19842004
    ReedWilliams19842004 over 9 years
    I will and I will. I'm assuming i can call: OpenSite.launch(OpenSite.class); from anywhere/any method, with the same results? I implemented the new invocation you suggested, it's a LARGE application so it'll take a min, but it's running/I'm testing it now.
  • ItachiUchiha
    ItachiUchiha over 9 years
    You can use CodeReview
  • ReedWilliams19842004
    ReedWilliams19842004 over 9 years
    Will there be a conflict if I have a main() in my major app class(as it's set up now),MrArray, and a main() in my OpenSite class? If I have to make a call to Application.launch(), how should that be arranged with my call to OpenSite.launch(OpenSite.class)?
  • ItachiUchiha
    ItachiUchiha over 9 years
    Why do you need a main() in OpenSite class?? I don't understand your question.
  • ReedWilliams19842004
    ReedWilliams19842004 over 9 years
    Sorry, I misread, I thought you wrote, I needed a main() in the class that extends Application. I understand now. However, still unclear on how to arrange both OpenSite.launch(OpenSite.class); & Application.launch() together. Could I just called Application.launch(), as is with no argument, from a public static void ()?