JavaFX: how to connect the main application object to the FXML controller - javafx

An FXML newbie question. I have a JavaFX Application class which does not do a whole lot (anymore) other than setting up the scene, and a controller class which gets instantiated when the FXML file is loaded and where I moved much of the logic (because seemingly the main class and the controller class are not really aware of one another).
However, I have a method which sets up setOnDragDropped on the scene. Normally, my main app would handle drag events, but because of FXML, that logic needs to be in the controller now (for File->Open for example). I have not found a way to setup DragDrop from the controller because the "scene" is only known in the main class. And since I don't instantiate the controller directly (indirectly instantiated when FXML file is loaded), I can't pass it object references easily.
How can I link the main and controller so I can have a bit more flexibility in where I put the logic? Or how can I migrate the scene dragDrop handler to the controller?
Thanks

Related

Why is JavaFX singleton?

I would like to have a Java GUI library allowing something like this:
Window window1 = new Window();
window1.show();
Window window2 = new Window();
window2.show();
...
But a JavaFX window needs to be started within its own class extending Application and cannot be instantiated several times.
Why can a JavaFX window not be instanciated like other classes? Why is it called Application and not Window or Frame? Why does JavaFX breaks OOP? Why do we have to use singletons mixed with reflection? I want no magic, I want an OOP-behaving window object appearing on the screen.
But a JavaFX window needs to be started within its own class extending Application and cannot be instantiated several times.
This statement is wrong, there are restrictions on when you can invoke the constructor on the Window class, but they are not related to extending the Application class.
Why can a JavaFX window not be instanciated like other classes?
The constructor of the Window class is protected, so it can be "used only within the package and in sub-classes outside the package".
Instead, in your application, instantiate a class which is a sub-class of Window. The appropriate class to use is Stage. The constructors of Stage are public rather than protected like Window.
So, to show two stages (which are windows), you can write:
Stage stage1 = new Stage();
stage1.show();
Stage stage2 = new Stage();
stage2.show();
Why is it called Application and not Window or Frame?
An application is called Application because that what it represents. Within an application, you might have multiple Stages (which are instances of Windows), so it would not make sense to call an Application a Window. Anyway, an Application has a different purpose: It's main purpose is to implement the Application lifecycle interface and processing as described in the Application javadoc.
Why does JavaFX breaks OOP?
It doesn't. It uses a protected modifier on Window to enforce encapsulation, which is a key hiding technique of OOP. It provides subclasses of Windows via inheritance for different purposes such as Stages, which are your standard application windows, and PopupWindows, which are specialized window subsets helping with tasks such as creation of popup lists for context menus and combo boxes. It implements Separation of Concerns by differentiating between Application and Window functionality within separate class structures.
Your other questions and statements make no sense to me.

FXML can't see new methods

I'm developing a gui for a college work and i'm using JavaFX and FXML, everything was fine until the fxml get angry.
The fxml file does not see the new methods of my controller class anymore. Yesterday, when i stopped, it was 5 methods, today i'm trying to add more methods but the fxml does not see these new methods. The scene builder doesn't show them either, only the older ones.
And the funniest part is: This only occurs with this FXML file and its controller. Others FXML files and its controllers works perfectly.
Any help?

Run code on start with javaFX 2

I would like to know how I can run code with the start of JavaFX 2 applications.
For example the Hello World application in Netbeans. How can I set elements invisible on start?
I use JavaFx with FXML and SceneBuilder.
With java I could just use "edit code" in Netbeans to put my code in and it would run on start.
With fxml you can attach a controller whose initialize method is invoked when the document is loaded (this would be the preferred way for me to initialize fxml based UIs in code).
JavaFX applications have a start method for processing initializing the GUI on startup.
In your fxml you can set the visibility property on a Node to define it's initial visible state.
You can create a CSS stylesheet and in that setup a selector which selects your node and does initial styling on it such as making it invisible.
FXML also allows you to embed scripting languages in the fxml document or external files which can be used to initialize the GUI and act on events.

How do I get the root display object of a MovieClip to be a MovieClip object with Flex?

I am trying to build an MXML application with Flash Builder 4.5, and I am integrating an API that requires the root of a display object to be a MovieClip. Personally I think this is bad design but I have to go with it. The root object always seems to end up as the stage, because of course I have to add the object to the stage for it to be added to the display list. Is there some way that I can either change the stage type in an MXML application to a MovieClip or is there some way to force a MovieClip wrapper to become the root of a display object?
Both mx Application and Spark Application inherit from Sprite, not MovieClip. Take a look at the inheritance chain at the top of
mx Application docs
Spark Application docs
The third-party API can't cast your Flex movie to MovieClip, because it's not one.
HTH;
aMY

buttonBar custom component

I am using a custom component at the top of my application that includes an image and a buttonBar. I have the namespace declared in main.mxml as comps and the package is components. When I assign the dataProvider to the buttonBar and run the application, all I get is a blank page. If I remove the dataProvider everything loads fine. the dataProvider is supposed to be the ViewStack component I have in the main.mxml. The buttonBar and image custom component is TopNav.mxml
The problem is that my TopNav component does not know the dataProvider I am asking it to change; MyViewStack. Do I need to create a public viewstack variable and pass it to the component?
Your application is probably throwing a runtime error. Do you have Flash Player debug installed? You can see it here: http://kb2.adobe.com/cps/155/tn_15507.html
If your application has a runtime error, it won't continue executing code. You probably have a null pointer somewhere.

Resources