Create Pane level controllers - javafx

I have an accordion control with 14 panes. Is there a way to attache a separate controller to each pane? The FXML allows me to specify a controller, but the loader rejects it.

Related

JavaFX - How to load a specific AnchorPane's contents from an FXML file?

I'm trying to make an application which switches between scenes back and forth however I need to load a specific AnchorPane's contents into another AnchorPane when the scene switches back. For Example:
In my FXML1, I have a hierarchy that looks like this:
AnchorPane0
----SplitPane
--------AnchorPane1
--------AnchorPane2
In FXML2 the hierarchy is just this:
AnchorPane0
So I load FXML1, then I have a button that switches scenes loading FXML2.AnchorPane0 into FXML1.AnchorPane2. I have a back button in FXML2.AnchorPane0 that needs to load the original scene of FXML1.AnchorPane2 into FXML1.AnchorPane2. Right now my back button loads all 4 containers of FXML1 into FXML1.AnchorPane2. So my questions is, how do I load a specific container's contents preferably without making FXML1.AnchorPane2 its own FXML? Do I need to write a get method for the FXML1.AnchorPane2 to access its contents or is there a way to return an AnchorPane with all of its contents in place already?
I found the solution as shown below:
AnchorPane loader = FXMLLoader.load(getClass().getResource("myFXML.fxml"));
SplitPane spane = (SplitPane) loader.getChildren().get(0);
AnchorPane pane = (AnchorPane) spane.getItems().get(1);
foregroundAnchorPane.getChildren().setAll(pane);

JavaFX Scenebuilder: How to connect pages with each other?

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).

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

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

What is the justification for JavaFX FXML schema not supporting context menu inside the panes?

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?

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.

Resources