TreeView in FXML - javafx

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.

Related

How to call a function inside a nested controller when catching an event in the parent [duplicate]

This question already has answers here:
JavaFX8 fxml naming of nested controllers
(1 answer)
JavaFX Nested Controllers (FXML <include>)
(2 answers)
Closed 22 days ago.
I'm currently writing a JavaFX application with nested Views and Controllers and I need to execute a function in the nested controllers when a specific event occurs in the parent one.
The FXML for the main window is as is
<TabPane fx:id="viewsTabpane" side="RIGHT">
<Tab fx:id="graphicViewTab" closable="false">
<graphic>
<ImageView>
<Image url="#../Images/Graphic_Icon.png" requestedHeight="40" requestedWidth="40"/>
</ImageView>
</graphic>
<fx:include source="GraphicView.fxml"/>
</Tab>
<Tab fx:id="xmlViewTab" closable="false">
<graphic>
<ImageView>
<Image url="#../Images/XML_Icon.png" requestedHeight="40" requestedWidth="40"/>
</ImageView>
</graphic>
<fx:include source="XMLView.fxml"/>
</Tab>
</TabPane>
When switching from the xmlViewTab to the graphicViewTab I need to execute a updateModel() function from the XMLViewController.java file.
Any idea on how to do it ?
Thanks

How to use the same component in more than one place in JavaFX?

I want to use the same MenuItem in a MenuBar and in a ContextMenu (both have the same text, will be enabled/diSabled under the same conditions, and will perform the same task). How can I achieve this in FXML in the same file?
I tried using <fx:reference> but it only shows up in the first place I deference it.
<MenuBar>
<menus>
<MenuItem fx:id="menuToDuplicate" />
</menus>
</MenuBar>
.
.
.
<ContextMenu>
<items>
<!--Same MenuItem as above -->
</items>
</ContextMenu>

How can I add items to a ComboBox (or other Control) using FXML?

Recently, I discovered that <ComboBox>—and other controls—can have an <items> element underneath them .
How can I populate, or add items right to a control in the FXML markup?
(One use case for this might be to make the FXML semi-functional as a mockup to show to stakeholders.)
Research proves that it's done with a combination of the fx:value and fx:factory attributes. These seem to have been added in JavaFX 8 JavaFX 2.
Below, I'll cite the mechanisms and then give some examples.
🦶🔫 Warning:
Note, as #fabian does, that though this works well in the short term for something like a prototype or mockup, adding items directly to the FXML breaks the separation between model and view—and that may likely be an undesired result in the long term.
The Mechanisms
fx:value
The fx:value attribute can be used to initialize an instance of a type that does not have a default constructor but provides a static valueOf(String) method. For example, java.lang.String as well as each of the primitive wrapper types define a valueOf() method and can be constructed in FXML as follows:
<String fx:value="Hello, World!"/>
<Double fx:value="1.0"/>
<Boolean fx:value="false"/>
Custom classes that define a static valueOf(String) method can also be constructed this way.
Source: JavaFX 2 Introduction to FXML
fx:factory
The fx:factory attribute is another means of creating objects whose classes do not have a default constructor. The value of the attribute is the name of a static, no-arg factory method for producing class instances. For example, the following markup creates an instance of an observable array list, populated with three string values:
<FXCollections fx:factory="observableArrayList">
<String fx:value="A"/>
<String fx:value="B"/>
<String fx:value="C"/>
</FXCollections>
Source: JavaFX 2 Introduction to FXML
Some Examples:
ComboBox
<ComboBox value="One">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Three"/>
<String fx:value="Two"/>
<String fx:value="One"/>
</FXCollections>
</items>
</ComboBox>
CheckComboBox
The ControlsFX Controls are a little different:
<CheckComboBox>
<items>
<String fx:value="One"/>
<String fx:value="Two"/>
<String fx:value="Three"/>
</items>
</CheckComboBox>
TableView
TableView gets a little more complicated because it needs CellValueFactorys to know which part of the Person to show in each column.
<TableView prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn text="Name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Comment">
<cellValueFactory>
<PropertyValueFactory property="comment" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Person name="Jacob" comment="Hey!"/>
<Person name="Isabella" comment="Dude, we're in FXML!"/>
<Person name="Ethan" comment="No way!"/>
</FXCollections>
</items>
</TableView>

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.

Gluon SceneBuilder 8.1.1 Problems using Include or Import FXML

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:

Resources