0

This is my current project structure:

guiPackage
    Window
    WindowController

mainPackage
    Main

The Main class handles my main program and logic (in main method).

The Window class is just a basic window with a label.

How would I update the label text from my Main class? I assume I need to access the controller and pass data to it.

Is this the correct structure for a JavaFX project or is there a better way of doing things?

Edit: This is the code from my main method:

    Window.launch(Window.class);

    FXMLLoader fxmlLoader = new FXMLLoader(Viewer.class.getResource("/gui/fxml/Viewer.fxml"));
    ViewerController controller = fxmlLoader.getController();
    controller.setLabelText("Test");

This doesn't appear to change the label text at all.

Dotl
  • 1,406
  • 1
  • 13
  • 16
  • See http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 – James_D Jul 13 '15 at 11:33
  • I've followed that tutorial with no luck. See my edit above. – Dotl Jul 13 '15 at 12:33
  • You have to call `getController()` after you call `load()` (since the controller is defined in the FXML file, the loader won't know its controller until it's loaded the file...). Also, this should not be in your `main(...)` method, since that isn't on the FX Application Thread. You should really think of the `start(...)` method in your application class as the replacement for the `main(...)` method in a "regular" Java application; perhaps just move your application logic from `main(...)` to `start(...)`. – James_D Jul 13 '15 at 12:34
  • Thanks for your help. Why shouldn't this be in the main method? The main method is where my main program is - how else would I transfer data from the Main class to the Window class? Should I instead move everything from main method to the Window class? Wouldn't that defeat the point of separating logic from GUI? – Dotl Jul 13 '15 at 12:43
  • I just read your edit, thanks. I'll move everything from the main method to the Window `start(...)` method and give this another go. – Dotl Jul 13 '15 at 12:44
  • You can still separate things, but use the `start(...)` method as the launch point instead of the `main(..)` method. Neither one should contain much code, no matter what the application. – James_D Jul 13 '15 at 13:06

0 Answers0