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?
Related
I am working on a JavaFX project and the problem that I am facing is that I can't connect the different pages for my application. I can't go from another FXML file to another FXML File. For example I have two FXML Files. One for the Login Screen and one for the Menu. What I want is that when I click on the login button of my Login screen that I immediately go to my Menu Screen.
One way to transition from one screen to another is to assign a new Scene to a Stage using the setScene method of Stage.
Another way is to assign a root node like BorderPane to the Scene and swap FXML using its set methods(setTop, setCenter etc.) as per your application needs (In your case, on logging in).
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
It is possible to add a context menu to a scroll pane, but not to other types of panes. Why?
How FXML Works
FXML works by introspecting on the Java API using reflection (or by using specialized builder classes). More information on FXML works can be found in the Introduction to FXML documentation.
Why ContextMenus can't be defined on Panes in JavaFX using FXML Markup
Control has a contextMenu property. A ScrollPane is a Control. Other pane types such as StackPane are not controls. As there is no corresponding property in these other pane types which could be set to contain a reference to a contextMenu, you can't define a contextMenu on these pane types using FXML.
For similar reasons, you can't define a Tooltip on a Pane either.
How to define a ContextMenu for a Panes in an FXML Controller
You can still set a context menu on panes (and any other arbitrary nodes which are not controls) via code, using the contextMenu show API, for example by placing the following code in your FXML controller.
#FXML StackPane stack;
// . . .
public void initialize() {
final ContextMenu contextMenu = new ContextMenu(new MenuItem("xyzzy"));
stack.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
contextMenu.show(
stack,
mouseEvent.getScreenX(),
mouseEvent.getScreenY()
);
}
});
}
Why not add a ContextMenu property
Node could have a contextMenu property, which would allow ContextMenus to be defined on Panes via FXML Markup.
The reason why Node does not have a contextMenu property but Control does is because ContextMenu is itself a Control. Adding a ContextMenu property to node would mean that the core scene graph model code for the JavaFX implementation would have a dependency on the controls module (which would have a dependency on the scene graph module), hence a circular dependency. This would prevent the shipping of a very light Java runtime system which included the core JavaFX scene graph and rendering engine but did not include controls (not that anybody ships such a system today).
How to File Feature Requests
If you think the system should be changed to allow definition of context menus on arbitrary panes using SceneBuilder, then you can file a feature request against the JavaFX issue tracker (if you do so, include a link back to this question in the feature request).
Described method to open popup leads to multiple popups open if every node in the scene graph want to open context menu. Consuming of event is definitely needed.
See also discussion at Using FXML to Create ContextMenu within a Pane It provides working answer to this problem.
BTW, Node.onContextMenuRequested(...) should be used instead, yes?
Actually I want to switch from one scene to another scene on particular event. So i can do that by setting scene of the stage without binding but i want to bind my stage with scene as soon as a flag changes my scene will change automatically....pls help
The JavaFX Demos and Samples Downloads contains an application called FXML-LoginDemo. It demonstrates how to change scenes in a stage. The important part happens in the method replaceSceneContent. As for automatically changing the stage when a flag in your application is updated, there are several ways to do that. I recommend using a PropertyChangeListener or using JavaFX withCDI and then using CDI events. I used the latter approach in my application and it works really well.
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.