0

I'm working on a project for school that requires a GUI so I decided to learn JavaFX. With Swing you can construct a gui in the controller like this

public class Controller
{ 
   public Controller ()
   {
      // insert code
   }
}  

And call it in your constructor

public class Constructor
{
   public static void main(String[] args)
   {
      Controller GUI = new Controller();
   }
}

I've been trying to use this setup with JavaFX with little to no luck, is it even possible?

fabian
  • 80,457
  • 12
  • 86
  • 114
  • 1
    My advice: Lose the mindset of how things were done in another framework and learn the proper way to do things in the JavaFX framework. This will save you a lot of headaches. – SedJ601 Mar 19 '18 at 14:41
  • you probably know but: the javafx tag info has references to decent tutorials, be sure to read one :) – kleopatra Mar 19 '18 at 15:39
  • See if https://stackoverflow.com/a/32465949/2189127 helps (it is answering a somewhat different question, but the basic structure there might suggest how to approach this in a nice way). – James_D Mar 19 '18 at 17:12

1 Answers1

1

When you will run a JavaFX application you must extend the Application class in your Main class:

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

public class Main extends Application{ //extends Application
  public Main() {
    launch(args);  //start window
  }

  @Override
  public void start(Stage primaryStage) throws Exception {
    //code here
  }
}

Now you must override the start method. In this start method you have as parameter the Stage primaryStage, this is your Window. To call thestart method you must call: launch(args) in main method.

Now you can add your Code in the start method.

When you will create your GUI in another class like Contoller than you have different ways to do this. First we look on the Stage which in the Constructor. To display the stage you first need to set a Scene to the Stage:
stage.setScene(Scene scene);
To create a Stage you must add a Parent object in the Constructor
new Scene(Parent root);
Some Parent Object are Group and Region
Further subclasses of Parent are: Pane, GridPane, BorderPane, Chart, TableView, ListView and much more.

We have a Stage from the Start Method so we now need a Scene We can create this in the start method and add it to the Stage:

Scene scene = new Scene();
primaryStage.setScene(scene);

This code doesn't run because the Scene() need a Argument of type Parent Now we can create our Controller class in which we make our Parent object with all our GUI components. For this we create a Parent object (in this case BorderPane) in our Controller class:

public class Controller {

  private final BorderPane borderPane;

  public Controller() {
    borderPane = new BorderPane();
    //gui code here
  }
}

Now we can create our gui in the Controller class.
Finnaly we make a function in the Controller class to get this Borderpane for our gui:

public Pane getParentObject(){
    return borderPane;
  }

In our start method we now set this Parent object to our Scene:

Controller controller = new Controller();
Scene scene = new Scene(controller.getParentObject());
primaryStage.setScene(scene);
primaryStage.show();

At the end we only need to show our Stage.

I would recommend you to search for some tutorials with JavaFx there are a loot of good video or read tutorials in the Internet

I hope I could help you

Morchul
  • 1,987
  • 1
  • 7
  • 21
  • Why? Should I better create a Stage object in the class and use this? – Morchul Mar 19 '18 at 15:23
  • because you should _never_ (as in NEVER-EVER) extend any class if a) you can reach the same goal by configuration b) an extension wouldn't enhance the class' inherent functionality – kleopatra Mar 19 '18 at 15:29
  • also: you seem to try inverting the sequence of control in your snippet - you create a Controller (which is-a stage) in the Application's start, and then? How does that relate to the primaryStage, passed in by the application and expected to get a scene? Ahh .. seeing your edit: you are on the way ... but this comment still holds: why create a new stage? The application gives you one to fill :) – kleopatra Mar 19 '18 at 15:35
  • In my case, I would create a Class with a BorderPane and add this to the primaryStage. But his question was to do this in one class so I gave him an example to do all stuff in the other class like he show it in his example. Finally I recommend him to look for some tutorials because I now it has better ways as this example to do it. Ok I agree with you that this code doesn't run because I doesn't set a Scene etc. – Morchul Mar 19 '18 at 15:41
  • the first part of your answer is fine, the second is just plain wrong - so you might consider to edit (or even remove because it it rather unclear what the OP is asking) the second part :) – kleopatra Mar 19 '18 at 15:44
  • Maybe I should rephrase this, I'm sorry. All I want to know if there's a way I can create/setup the JavaFX GUI in a different class then call it in my main class. Does that make more sense? – Ryan Akuszewski Mar 19 '18 at 15:52
  • @RyanAkuszewski I'm not sure I understand you, you can extend Application also in other classes and call `launch` from there. – Morchul Mar 19 '18 at 16:08
  • yeah, improved :) now we need to wait for a clarification of what @RyanAkuszewski really wants - don't get it: you (ryan) certainly can call whatever ui-factory and add its output to the stage in the start method, so where exactly is the problem? Still suspect that you need to learn some fx basics, particularly on how the winds blow :) – kleopatra Mar 19 '18 at 16:41
  • @kleopatra I'll send you the project that's not working when I get out of class(Sorry should've done that to begin with) – Ryan Akuszewski Mar 19 '18 at 17:23
  • @Morchul I'm sorry I'm being so confusing, I'll send you the code when I'm out of class, that should clear it up – Ryan Akuszewski Mar 19 '18 at 17:24