2

Working on a JavaFX application for a school project and struggling mightly with trying to get this to work...

I have JavaFX application that launches a main menu screen. From that screen, there are option buttons to launch other screens that work with my application's various classes. It's the simple, classic CRUD app that all CS students have to do.

My question is, I want to compartmentalize the various CRUD screens into their own JavaFX classes (i.e. create.java, update.java, etc). Then my main JavaFX menu screen would call those classes.

Please note, although these classes are all their own separate JavaFX classes, they should all work together as a SINGLE JavaFX application. In other words, they'll be all working together to update the same object (e.g. a Car object).

I've learned that there should only be ONE JavaFX Application (i.e., the other classes should NOT have "extends Application'). My main menu JavaFX screen should be the only one to have 'extends Application'.

OK here's the code for the Main Menu screen:

public class My_Project02 extends Application {

    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {

        // Hold two buttons in an HBox
        HBox hBox = new HBox();
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER);
        Button btCreate = new Button("Create");

        hBox.getChildren().addAll(btCreate);

        // Create and register the handler
        btCreate.setOnAction((ActionEvent e) -> {

            // ** WHAT DO I PUT HERE TO LAUNCH THE JavaFX CreateStage.JAVA CLASS? **
            // I added this line below....
            CreateStage createWindow = new CreateStage(destroyerList);

            // Now this event handler works

        });

        // Create a scene and place it in the stage
        Scene scene = new Scene(hBox, 300, 50);
        primaryStage.setTitle("TITLE"); // Set title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }
}

/**************************
HERE'S THE CreateStage.JAVA FILE
How do I set this up to get this to work from My_Project02?
**************************/

public class CreateStage {

        // Added this line below and now this CreateStage class can be launched from My_Project02 application above
        Stage primaryStage = new Stage(); 


        Scene scene = new Scene(new Button("OK"), 200, 250);
        primaryStage.setTitle("MyJavaFX"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage

}

-- END --

Please Help! Thanks much in advance to all who reply.

72909903
  • 61
  • 1
  • 7
  • You should only have one `Application` subclass in your application (hence the name: it represents the entire application). If you want a class to be reusable in other applications, it should not be a subclass of `Application`. – James_D Oct 03 '17 at 23:11
  • Thanks much James_D for getting me going in the right direction. My question has been edited to reflect your help. – 72909903 Oct 04 '17 at 01:14
  • Can you edit it so `CreateStage` will at least compile, so we know what you are intending to do. – James_D Oct 04 '17 at 01:16
  • Ah, got it now. Please see my edits with comments to my post. Sorry, been banging my head coding non-stop for several hours but a nice break in the action, along with your help, rejuvenated me and helped get me to the finish line. Thanks again James_D, much appreciated! – 72909903 Oct 04 '17 at 03:24

0 Answers0