JavaFX Scene Builder 2.0 "Failed to include '*.fxml' " - javafx

In Scene Builder, I included many fxml files in Tabs, but now, I can not include any. Also, if I delete my existing fxml file which doesn't have any problem, and then add again by using Scene Builder's include function(File->Include->FXML..), I get the same error:
"Failed to include '*.fxml' under Tab."
EDIT: I get same error in gluon scene builder 8.3.0.

I experience the same issue with SceneBuilder 8.3.0, Eclipse Neon + Oracle JDK8 on Linux.
However, using a phrase as such
<Tab fx:id="tab1" text="Untitled Tab 1">
<content>
<fx:include id="tab1" source="Tab1View.fxml"/>
</content>
</Tab>
directly in the FXML file, it works as expected.
PS: I am wondering whether this is a bug or an unwanted feature.

Related

JavaFX: Include common FXML file from common lib in current FXML

We have multiple GUI project based on JavaFX. Our goal now is to refactor a lot of these GUI's and to extract common views and source code and make this common code available through libs (bundle jar's). I currently have the problem that I have a View which displays log files and which is included via an FXML in the main view
<fx:include fx:id="logFileView" source="./sub/LogView.fxml"
maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="600" prefWidth="1000"
style="-fx-background-color: rgba(0, 0, 0, 0.0);"
StackPane.alignment="CENTER_RIGHT" />
The LogView.fxml incorporates it's controller
fx:controller="auv.e4.application.analyser.controller.sub.LogViewController"
but this LogView.fxml is now placed in a common lib (bundle jar) and should be accessible by any of our GUIS's
We have like 50 different views which are structured this way and I would prefer using the FXML this way.
Another problem is the common lib will be bundle project (otherwise the RCP GUI can*t resolve them as dependencies) which makes using absolute paths impossible (Which is alo not a good practice). The GUIs are RCP based GUI's which use JavaFX for rendering.
My question is now how do I reference the LogView.fxml from an another project from inside an FXML (with fx:include) ? Is this possible ? If not what kind of restructuring should /can be be done ?

Scene Builder 2.0. FXML import not working

I'm using JavaFX Scene Builder 2.0. version and I would like to import a FXML file under TabPane controller. My idea is that I will have one kind of container FXML file with TabPane and separate FXML files for things that will come inside of tabs. When I try to import FXML file where I have things that I would like to add under one tab, Scene Builder gives error message "Failed to import TabContents.fxml under TabPane". Why it is not possible to import anything under the TabPane? If I import same file directly under root StackPane everything works.
EDIT: While the below works, it is best to add some sort of layout pane (like an AnchorPane) to each tab in your TabPane, select the AnchorPane pane in the hierarchy view on the left and then import your other fxml document into that, as opposed to importing directly into a TabPane (Perhaps if the root node of the document is a Tab it would work?).
Looks like a bug, for now:
Cut your entire document hierarchy
Add an empty stack pane
Include your other FXML file
Paste your document hierarchy into the stack pane
Drag your FXML file into the appropriate place in your hierarchy
Cut your new hierarchy (with your file included)
Delete the now defunct stack pane
Paste your hierarchy back in
And voila, working as intended!
Three simple steps
Add a StackPane to the content area of your Tab, ensuring that it occupies 100% of the area.
Go to the Hierarchy view and select that StackPane you just added.
Include your TabContents.fxml. It should automatically be added to the created StackPane and should display as desired.
Also, to answer your additional question, importing it directly into the TabPane will render it as a Graphic rather than a Node.
just add a new TabPane where you want and then edit your FXML code of the TabPane just added as following:
<TabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="5000.0" prefWidth="5000.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab closable="false" text="MyTab1">
<content>
<fx:include source="MyTab1.fxml" />
</content>
</Tab>
<Tab closable="false" text="MyTab2">
<content>
<fx:include source="MyTab2.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
adding as many tab you need just adding more 'Tab' tags.
'MyTab1.fxml' and 'MyTab2.fxml' are just fxml files contained in the same folder of the fxml file of the TabPane.
it works for me selecting first the root node in the scene. Then I import de FXML and it is imported hanging from this root node. As the last step, I move the imported FXML to the place I want in the scene.
I realised that the FXML import can also fail when you have another FXML template included in the one you want to import. Something like:
<fx:include source="top-menu-bar.fxml" />
Looks like SceneBuilder doesn't know where is top-menu-bar.fxml. The solution can be to work on the template separately and then adding those includes manually to make it works.
It works for me:
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" >
<children>
<fx:include fx:id="includeID" source="includeSource.fxml" />
</children>
</AnchorPane>
The include file must be include in the children of a pane.

get FXML file nodes using java code

How to get elements or nodes from FXML file using Java, I know the way by using initialization or by setting controller class in FxmL . But I need to do it without any controller. I want to access the nodes inside the fxml file using.
My FXML COde:
HBox fx:id="hbx" id="hbx" alignment="CENTER_RIGHT" prefHeight="100.0"
prefWidth="200.0" BorderPane.alignment="CENTER"
My java Code
System.out.println(par.lookupAll("hbx"));
See my Code above, could you give me a hint?
After loading the FXML file, you can use Node#lookup():
Node node = fxmlParentPane.lookup("#nodeId");

What is wrong with my syntax calling a stylesheet (css) from an FXML file?

I am working in NetBeans 8, with Java 8 / JavaFX and FXML.
In the first controller (an AnchorPane) in my FXML document, I have this, that works:
stylesheets="file:///C:/Users/me/Documents/NetBeansProjects/MyApp/src/myApp/myStyle.css"
However, when I attempt to replace that with any type of relative path, I get the following error at runtime:
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "myStyle.css" not found.
I tried
stylesheets="myStyle.css"
stylesheets="file://myStyle.css"
stylesheets="./myStyle.css"
stylesheets="css/myStyle.css"
(where I put a copy of the css file in a subdirectory from where it was)
...and maybe 50 other permutations. Every time I get the same error.
The reason I care is that I am having a second problem. In the first version, that works on my machine... when I take my jar file to another machine, the css is not applied when the application is run... It's like the resource file isn't copied.
Help?
Thanks!
-Adeena
Let me consider your project structure as follows
MyApp
|
|_ src
|
|_controllers (Controllers)
|_view (FXML)
|_style (css)
|_myStyle.css
You can specific the relative path in the FXML using
<stylesheets>
<URL value="#../style/myStyle.css" />
</stylesheets>
In your controller, you can add it as follows
layout.getStylesheets().add(getClass().
getResource("/style/myStyle.css")).toExternalForm();
This is how it works for me:
<?import java.net.*?>
<fx:root type="javafx.scene.layout.BorderPane" ... xmlns:fx="http://javafx.com/fxml">
<stylesheets>
<URL value="#myStyle.css" />
</stylesheets>
</fx:root>
The css file is in the same package (folder) as the FXML. Also my root happens to be <fx:root>, I expect it to work the same for your <AnchorPane>.
Check out the docs, search for "Location Resolution" for details.

What are the pros and cons FXML?

What are the pros and cons of using FXMLs or not using FXMLs for developing JavaFX applications?
For developing enterprise JavaFX application which approach should one follow?
FXML Cons: It takes slightly longer to load and display.
FXML Pros:
Rapid scene development / mock up using Scene Builder.
FXML is not a compiled language; you do not need to recompile the code to see the changes. Just reload the FXML file.
It provides a clear separation of GUI from logic/controller.
So you can have different versions of a scene/view using the same controller. This is handy for demo's for instance.
The content of an FXML file can be localized as the file is read.
Definitely use FXML in enterprise apps !
I would add two contra to Jurgens list.
If you are working with FXML instantiation of your view is sort of inconvenient. At least from my point of view.
Node explorer = new MyExplorerWidget();
or
Node explorer = cdicontainer.newInstance(MyExplorerWidget.class);
is more pleasant than
FXMLLoader loader = new FXMLLoader(getClass().getResource("com.mycompany.some.very.long.name.MyExplorerWidget.fxml"),explorerwidgetresouces);//Of course we want our app internationalized
Node explorer = loader.load();
Another point is that FXML is static. If you want to generate your UI at run time depending on some model you will write UI code anyway. I ended up with useless fxml files like this PropertyGrid.fxml
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="PropertyGridController">
<children>
<VBox fx:id="vbox" layoutX="63.0" layoutY="-28.0" prefHeight="172.0" prefWidth="163.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
And the PropertyGridController.
public class PropertyGridController{
#FXML
VBox vbox;
....
public void setModel(PropertySheet model){
//.... tons of code to generate the actual property grid and add it to the view
}
}

Resources