0

I am writing a very basic 3D modelling program. I've used LWJGL to render my objects and JavaFX to provide an user interface in a separate window, new thread.

As I saw JavaFX likes to take control over the application, but in my case this was not an option. I tried to pass my already created scene graph to the JavaFX controller class, but I didn't find a way doing this properly.

It's seems to be impossible to pass anything from outside into the main JavaFX class. The start method loads the layout from an FXML file with reflection magic, but this method is called in the constructor, therefore variables are not initialized. Defining a new constructor with parameters throws an exception (class cannot be initialized).

After struggling many hours, I gave up, I've decided to create a new scene graph in the JavaFX controller and created a getter method to it.

public class Toolbox extends Application implements Runnable {

    private ToolboxLogic logic = new ToolboxLogic(); //controller, the scene graph is instantiated

    ...

    public SceneGraph getSceneGraph() {
        return logic.sceneGraph; // returns the scene graph
    }
}

Not a beautiful solution, but it shall work, I said. But it doesn't. I tried to load a file in two locations:

  • with code written in the LWJGL renderer
  • with buttons, calling a method in the controller

If I load a file from the renderer, my objects show up on the screen, but I cannot navigate with the buttons, only the root node appears in the scene graph.

If I load a file from the user interface, I can navigate on the tree, but it doesn't show up in the renderer.

It seems like here

return logic.sceneGraph;

Java would have done a deep copy instead of returning a pointer, and each part of my program is working with its own version of the scene graph.

What is a problem, and how can I make it work properly?

Thank you!

  • I know nothing about lwgjl, so I don't know what constraints it puts on your application structure. The intention in JavaFX, however, is that the `start(...)` method should be the entry point (startup) for your application: i.e. it is the equivalent of (or replacement for) `main(...)` in a "traditional" Java application. So basically there is nothing to pass to the application subclass: anything you need is created from that point on. See if http://stackoverflow.com/questions/32739199 or http://stackoverflow.com/questions/32464698 help. – James_D Dec 15 '15 at 13:26
  • I've seen that JavaFX tries to be the entry point in the application, but my design could not be rewritten to use entirely JavaFX's approach. I got around this problem by using a static variable, it is good for my project, but is a really ugly solution. Although, I could not understand why it behaves this way, why I could not get a variable when it was non-static. Thank you anyway! – danalizieors Dec 15 '15 at 22:56
  • Impossible to answer that without you showing how you are exposing the instance of `Toolbox` that is created when you call `launch()` (assuming you call `launch()` somewhere). If you can't use the standard approach, you can use the technique in my answer to the first link in my comment above. – James_D Dec 15 '15 at 23:06

1 Answers1

0

OK, I got it working by setting the scene graph static. Strange! Now, I'm really curious, why it behaves like this.