Gluon SceneBuilder 8.1.1 Problems using Include or Import FXML - javafx

I cannot include a simple fxml file into a BorderPane or TabPane.
Details including some sample code are already reported here:
https://bitbucket.org/gluon-oss/scenebuilder/issues/68/various-problems-trying-to-inlcude-an-fxml
I still have a few questions according this topic:
Is there a limitation of containers where you may fx:include into ? (e.g. are TabPanes not allowed??)
Is there some specific thing I had to do inside the included FXML file which I am missing?
Any other hint?
If you experience the same problems - maybe you could vote for the bug, so we increase the chances to get a fix.

By default, when you include one FXML file into another using Scene Builder menu File -> Include -> FXML..., the included file is added under the root, according the following code:
final FXOMObject rootObject = targetDocument.getFxomRoot();
if (rootObject != null) {
final Selection selection = getEditorController().getSelection();
if (selection.isEmpty() || selection.isSelected(rootObject)) {
targetObject = rootObject;
} else {
targetObject = selection.getAncestor();
}
...
}
In the case of your AnchorWithTabPane.fxml file, if you don't select anything, it will be included under the root:
<AnchorPane ...>
<children>
<TabPane .../>
<fx:include source="UserControl.fxml" />
</children>
</AnchorPane>
Now, if you select TabPane, going through the else condition, it will go again under the root, giving that the tabPane's ancestor is the root itself.
If you select Tab, the ancestor is TabPane, but there you can't include a FXML node.
If you select the AnchorPane of a tab, the ancestor is the tab, and you can't include there a FXML node either.
So the solution or workaround in this case is adding some inner container or node to that anchor pane, and then selecting it: since its ancestor will be the AnchorPane, it will add the FXML node there.
And finally you can delete that temporary container/node.
As a result, you will have:
<TabPane ...>
<tabs>
<Tab text="Untitled Tab 1">
<content>
<AnchorPane ...>
<children>
<fx:include source="UserControl.fxml" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
Regarding your question about what containers can hold an fx:include, all the panes under javafx.scene.layout.* can hold them.
As hint, if you use NetBeans, edit an fxml file, and within the container tags click Ctrl+space, it will show you if the fx:include node is allowed:

Related

TreeView in FXML

How can I create a TreeView in FXML including all the branches and components in it? I can't find documentation about it. There is just code about how to populate it but I want to do it in FXML so y can have the design apart of the logic.
There's no specific documentation for FXML (beyond the Introduction to FXML document): but there is no need for any. An element beginning with an uppercase character is an instruction to instantiate the class of that name, so
<TreeView></TreeView>
will instantiate a TreeView. Attributes correspond to properties, so if you needed you could do
<TreeView editable="true"></TreeView>
Nested elements beginning with lower case also correspond to properties, and will be set to the enclosed FXML structure. So you can create a tree view with code like
<TreeView fx:id = "treeView">
<root>
<TreeItem fx:id="rootItem" value="Root" expanded="true">
<children>
<TreeItem fx:id="child1" value="Child 1" expanded="true">
<children>
<TreeItem fx:id="child11" value="Child 1.1"></TreeItem>
<TreeItem fx:id="child12" value="Child 1.2"></TreeItem>
</children>
</TreeItem>
<TreeItem fx:id="child2" value="Child 2">
<children>
<TreeItem fx:id="child21" value="Child 2.1"></TreeItem>
<TreeItem fx:id="child22" value="Child 2.2"></TreeItem>
</children>
</TreeItem>
</children>
</TreeItem>
</root>
</TreeView>
You can, of course, see all the properties in the API documentation.

I have chosen a Pane, but I would like to set the alignment, is it possible?

I'm doing a project with javafx. As cointainer I choose the Pane, maybe the worst decision! Is it possible to center the text, using the lenght of the sentence which will be loaded according to the size of the panel?
I'm actually showing the code that i wrote. As you can see, the GeneralQuestion lenght will be various, the question has not a min or max character. How can i center it according to the size of the panel?
This is how does it look like:
This is the output, as you can see it isn't centered. I know there's the VBox or other type of panel but i've already chosen this one and before changing everything i would like to know if there's any way to make it look better!
This is the controller:
public void nextQuest(ActionEvent e) throws IOException
{
secondaryImg.setVisible(false);
nextQuestionButton.setVisible(false);
getQuestion(cont);
cont++;
}
public void getQuestion(int cont) throws FileNotFoundException,
IOException
{
FileManager f = new FileManager();
numQuest();
QuestionManager m = new QuestionManager(f.loadQuestion());
GeneralQuestion.setText(m.getAllQuestion().get(cont).getBody());
answ=m.getAllQuestion().get(cont).getAnswer();
if(m.getAllQuestion().get(cont).getType().equals("normale")||m.getAllQuestion().get(cont).getType().equals("perditutto"))
answS.setVisible(true);
else if(m.getAllQuestion().get(cont).getType().equals("verofalso")){
RbVero.setVisible(true);
RbFalso.setVisible(true);
}
}
This is (a part of) the FXML:
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #b8ffe0; -fx-border-width: 8px; -fx-border-color: #0e3d73;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="guiz.controller.GameScreenController">
<children>
<Label id="numquest" fx:id="numquest" layoutX="286.0" layoutY="35.0" text="Label">
<font>
<Font size="15.0" />
</font></Label>
<Label fx:id="GeneralQuestion" layoutX="225.0" layoutY="123.0" style="-fx-font-weight: 800;" text="Quest">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</Pane>
In JavaFX you achieve a desired layout by nesting various layout panes inside each other. Pane is indeed a bad descision because a Pane just does not do any layout at all. In your case a VBox would indeed be a better alternative which you might put into a BorderPane.

How can I set a Tooltip max width and wrap in CSS in JavaFX [duplicate]

I encountered the following issue using JavaFX.
Redefinition of tooltip style using stylesheet works in Java Scene Builder.
Redefinition of tooltip style at execution in eclipse with the same stylesheet included in FXML file generated from scene builder with
<stylesheets>
<URL value="#../style/myCSS.css" />
</stylesheets>
does not work (any other property redefinition works).
Redefinition of tooltip style at execution in eclipse with same stylesheet using code instruction :
scene.getStylesheets().add(this.getClass().getResource("/style/myCSS.css").toExternalForm());
works properly.
Stylesheet used (myCSS.css):
.tooltip {
-fx-background-radius: 2 2 2 2;
-fx-background-color: linear-gradient(#FFFFFF, #DEDEDE);
}
.page-corner {
-fx-shape: " ";
}
AnchorPane {
-fx-background-color: firebrick;
}
FXML file used:
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="91.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml">
<children>
<Button layoutX="72.0" layoutY="35.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button">
<tooltip>
<Tooltip text="Tootip Text" />
</tooltip>
</Button>
</children>
<stylesheets>
<URL value="#../style/myCSS.css" />
</stylesheets>
</AnchorPane>
Edit: In other words I want to be abe to declare my stylesheet in the FXML file. Doing so seems to work for any property redefinition (AnchorPane background color in this case) except tooltips.
The CSS properties you are trying to set for the Tooltip are only relavent to JavaFX classes that extend the Region class. The Tooltip is a child of the PopupControl class and, as such, has a more limited CSS property library. Here's a link to a list of available CSS properties for Tooltip. That site is your best reference for JavaFX CSS properties.

FXML Style Not Applied To Dynamically Added TreeView TreeItem's

I am trying to apply css styles to A TreeView TreeItem but for some reason the item isn't picking up, and applying the style. Here is a snippet of the fxml file. As you can see the TreeView doesn't have any items, that is because I am adding them at a later time.
<AnchorPane styleClass="/media/css/TreeItem.css" id="AnchorPane" fx:id="main" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="db.manager.MainController">
<children>
<AnchorPane>
<children>
<TreeView editable="true" showRoot="false" />
</children>
</AnchorPane>
</children>
</AnchorPane>
I then have the following css in the file css file mentioned in the above fxml:
.tree-cell{
-fx-indent: 100;
-fx-underline: true;
-fx-background-image: url("/media/images/database.png");
-fx-background-repeat: no-repeat;
}
I am not getting any errors, so I know that the file is loading, but the style aren't getting applied to the newly added TreeItem's and I have no idea why.
You need
<AnchorPane stylesheets="/media/css/TreeItem.css" ... >
not
<AnchorPane styleClass="/media/css/TreeItem.css" ... >

JavaFX Tooltip customisation

I encountered the following issue using JavaFX.
Redefinition of tooltip style using stylesheet works in Java Scene Builder.
Redefinition of tooltip style at execution in eclipse with the same stylesheet included in FXML file generated from scene builder with
<stylesheets>
<URL value="#../style/myCSS.css" />
</stylesheets>
does not work (any other property redefinition works).
Redefinition of tooltip style at execution in eclipse with same stylesheet using code instruction :
scene.getStylesheets().add(this.getClass().getResource("/style/myCSS.css").toExternalForm());
works properly.
Stylesheet used (myCSS.css):
.tooltip {
-fx-background-radius: 2 2 2 2;
-fx-background-color: linear-gradient(#FFFFFF, #DEDEDE);
}
.page-corner {
-fx-shape: " ";
}
AnchorPane {
-fx-background-color: firebrick;
}
FXML file used:
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="91.0" prefWidth="200.0" xmlns:fx="http://javafx.com/fxml">
<children>
<Button layoutX="72.0" layoutY="35.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button">
<tooltip>
<Tooltip text="Tootip Text" />
</tooltip>
</Button>
</children>
<stylesheets>
<URL value="#../style/myCSS.css" />
</stylesheets>
</AnchorPane>
Edit: In other words I want to be abe to declare my stylesheet in the FXML file. Doing so seems to work for any property redefinition (AnchorPane background color in this case) except tooltips.
The CSS properties you are trying to set for the Tooltip are only relavent to JavaFX classes that extend the Region class. The Tooltip is a child of the PopupControl class and, as such, has a more limited CSS property library. Here's a link to a list of available CSS properties for Tooltip. That site is your best reference for JavaFX CSS properties.

Resources