Is there a way to insert children at specific position? - javafx

thats a part of my fxml:
<VBox>
<children>
<Button/>
<Button/>
</children>
</VBox>
I want to add a new child to vBox. But i want to add it at a specific position, for example between the two buttons "at position 2".

The child list of a pane is an ObservableList, which is just a subinterface of a plain old java.util.List. So you have access to all the usual list methods. Assuming you have a reference to the VBox in your controller, call it vbox, you can just do
vbox.getChildren().add(1, myNewButton);

Related

Same distance from AnchorPane to AnchorPane

I created this GUI in Scene Builder. I have an AnchorPane on an AnchorPane, but how do I get it to the edge of the same distance? See second picture. Up, down, right, left should be the same distance from the red AnchorPane everywhere.
-So first thing you need to do, is to choose the layout you want to adjust (the blue one).
-Under the Inspecter panel on the right, you choose the "Layout" pane
-Finally you set the disired values under Anchor Pane Constraints. (they need to have all the same value)
You can ignore all the steps and edit your FXML file :
<AnchorPane layoutX="31.0" layoutY="14.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: blue;" AnchorPane.bottomAnchor="ENTER_THE_VALUE" AnchorPane.leftAnchor="ENTER_THE_VALUE" AnchorPane.rightAnchor="ENTER_THE_VALUE" AnchorPane.topAnchor="ENTER_THE_VALUE" />

JavaFx: How to make an fx:include to fill the parent pane

I want to build a tab view. I like to put the content of each tab in a separate FXML file.
With <fx:include source="Details.fxml" /> I can import an FXML into my main FMXL. The import works fine but unfortunately the Details.fxml content is only using the prefHeight and prefWidth defined in the Details.fxml. How can I configure that the included FXML uses the entire of the parent (the tab pane).
My Details.fxml view contains a TableView and the table should always use the entire width of window. Also if I resize the window.
Thanks for any hints.
Regards,
Manuel

Included FXML does not fit parent correctly

I'm including several FXMLs in one main FXML view but the thing is when I change the height of the parent which is an AnchorPane the included view height doesn't change and I didn't find anything in the doc regarding the size of included FXML. Any idea anyone?
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<fx:include fx:id="child" source="child.fxml" />
</children>
</AnchorPane>
An AnchorPane will attempt to resize its children based on the constraints you set on those child nodes. From the documentation:
The application sets anchor constraints on each child to configure the
anchors on one or more sides. If a child is anchored on opposite sides
(and is resizable), the anchor pane will resize it to maintain both
offsets, otherwise the anchor pane will resize it to its preferred
size. If in the former case (anchored on opposite sides) and the child
is not resizable, then only the top/left anchor will be honored.
In the code you posted, you haven't set any constraints on the child node, so it will just be sized to its preferred size.
For example, if you wanted the child to stretch to fill the entire AnchorPane, then you would do
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<fx:include fx:id="child" source="child.fxml" AnchorPane.topAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.buttonAnchor="0.0" />
</children>
</AnchorPane>
Note that this also assumes that whatever node is generated by child.fxml is allowed to grow to the size of the AnchorPane (e.g. it's maxWidth and maxHeight properties have appropriate values), and that the AnchorPane also grows when the window is resized, etc. You might also want to consider whether or not an AnchorPane is the best choice, or if some other layout might work better.
In short, there is nothing special about using an <fx:include> in terms of layout: it behaves just like any other scene graph hierarchy.

What's the difference (in FXML) between layout.Pane and control.Control?

I am trying to learn how to create a UI. What I am currently experimenting with is a main screen on the left to house the current information for the user to see; and an Accordion on the right, for the user to select items which determine what is shown in the main screen. I am using Scene Builder to help me as I learn. When I drag a TitledPane (not empty) into my Accordion, it places an AnchorPane in there. This, unless I misunderstand something, does not allow for a list of selectable items in that TitledPane larger than the current height of the TitledPane. So I added another TitledPane (empty) into my Accordion, and then added a ScrollPane (not empty) to it. This again added an AnchorPane inside of the ScrollPane.
This is what my fxml document currently looks like (after the versioning, encoding and imports):
<VBox fx:id="testPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testfxml.FXMLDocumentController">
<children>
<MenuBar>
<menus>
<Menu text="File">
<items>
<MenuItem fx:id="1" text="1" />
<MenuItem fx:id="2" text="2" />
<MenuItem fx:id="3" text="3" />
</items>
</Menu>
</menus>
</MenuBar>
<SplitPane dividerPositions="0.75" prefHeight="600" prefWidth="800">
<items>
<StackPane fx:id="mainscreen" />
<Accordion fx:id="test">
<panes>
<TitledPane text="Test1">
<content>
<AnchorPane fx:id="test1" />
</content>
</TitledPane>
<TitledPane text="Test2">
<content>
<ScrollPane>
<content>
<AnchorPane/>
</content>
</ScrollPane>
</content>
</TitledPane>
</panes>
</Accordion>
</items>
</SplitPane>
</children>
When I look at the api for javafx (https://docs.oracle.com/javase/8/javafx/api), AnchorPane shows this:
Class AnchorPane
java.lang.Object
javafx.scene.Node
javafx.scene.Parent
javafx.scene.layout.Region
javafx.scene.layout.Pane
javafx.scene.layout.AnchorPane
While ScrollPane shows this:
Class ScrollPane
java.lang.Object
javafx.scene.Node
javafx.scene.Parent
javafx.scene.layout.Region
javafx.scene.control.Control
javafx.scene.control.ScrollPane
I'm assuming the formatting, which to me implies inheritance, is significant. I noticed that down through "javafx.scene.layout.Region" is identical, but that they differ after that. One goes to layout.Pane, the other to control.Control.
I've read through the descriptions on the api and, perhaps because of my obvious lack of experience with coding, it didn't quite make sense to me - perhaps I'm trying to use one of them incorrectly?
What's the difference? Is an AnchorPane required?
You can put anything you like as the content of a TitledPane. SceneBuilder (which seems unduly fond of AnchorPanes, for some reason) puts an AnchorPane as the default content; however you can remove it, or just choose "TitledPane (empty)" and add your own content. For example, you can remove the AnchorPane (or start with an empty TitledPane) and drag a ScrollPane there instead.
It actually sounds like you really need a ListView as the content of your TitledPane. A ListView comes already equipped with as-needed scroll bar functionality.
To answer your actual question, the API documentation is indeed indicating the inheritance hierarchy.
JavaFX (like most UI libraries) defines a rich inheritance structure. Parent is the superclass of anything in the scene graph that contains other Nodes. The vast majority of classes that subclass Parent also subclass Region, which represents something that takes up a particular space in the layout. (The exceptions are Group, which behaves somewhat differently with respect to layout, and WebView, which probably should be a subclass of Control, but isn't...).
Control and Pane are both subclasses of Region (and hence of Parent). Pane represents a "container": something that is merely designed to hold other Nodes and manage their layout. AnchorPane is a subclass of Pane. Control, by contrast, represents a UI "widget" (something with which the user directly interacts), so Label, Button, ComboBox, etc, are all subclasses of Control.
Some controls are actually quite complex, and contain other Nodes. ScrollPane is an example of a control, because the user can interact with it (via its scroll bars); it also contains another Node, called its "content". In the same way that you can use any Node as the content for the TitledPane, you can use any node as the content for the ScrollPane. The ScrollPane's content is the node the user views and moves around via the ScrollPane's scroll bars. Again, you can use any Node as the ScrollPane's content; SceneBuilder just chooses an AnchorPane as the default (for no real reason other than it has to use something).

JavaFX : Adapt component height to its children height?

I'm trying to develop a desktop app using javaFX (2.2), and I'm experiencing some difficulties to really get it to behave like I would like it to. Being a new user of the framework, I might not be using it as it should be...
What I want is a kind of dashbord, with several JavaFX or custom controls inside a ScrollPane. I currently have several views that look like this :
<AnchorPane id="userContent" xmlns:fx="http://javafx.com/fxml" fx:controller="squalp.view.OrgViewController">
<children>
<ScrollPane fx:id="scrollPane" styleClass="transparent" hbarPolicy="NEVER" vbarPolicy="AS_NEEDED" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<VBox fx:id="vBox" styleClass="transparent" alignment="TOP_LEFT" focusTraversable="true">
<children>
<Label text="Label" />
<Button fx:id="bTest" text="Test" onAction="#test"/>
<TreeView fx:id="orgTree" focusTraversable="true" />
</children>
</VBox>
</content>
</ScrollPane>
</children>
</AnchorPane>
As long as I keep using simple controls, no problem, items are correctly placed one after another in th VBox.
But when using TreeViews or TableViews, I can only get my controls to have fixed height, meaning extra space on the bottom, or new scroll bars, in addition to the main one. What I would like to achieve is to adapt my controls to their current content height.
Qt's QML, for exemple, has a childrenHeight property, and it's kind of what I'm looking for. I figure this content height must be calculated somehow, since components scrollbars do adapt to the content.
I had to use a workaround for tableviews :
table.setPrefHeight(list.size() * rowHeight + headerHeight);
but I couldn't find a way to track displayed items on a treeview.
Has anyone an idea of what I could be doing to handle this issue ? Or I am really not using the framework as I should ?
Thanks in advance.
EDIT : Visual description of the problem :
Case 1
Here my treeview's height is set to a default value. There are not enough items to fill the treeview : if I want to put another component below, there'll be a gap; if my scene is not big enough, the ScrollPane's scrollbar appears, even though all the displayable content is visible.
Case 2
If if expand my treeview's items, there are now too many of them. They are not all visible at once, and the treeview's scrollbar appears, even though I already have a scrollable view with the scrollpane.
What I would like is my TreeView to adapt to its content height (reduced height if items are collapsed, bigger height if they are expanded), in order to be able to put several components one after another and to scroll through them all using the ScrollPane's scrollbar.
Since the TreeView component has itself a scrollbar, its visible content's height must be calculated at some point (since scrollbars basically use ratios based on contentHeight/componentHeight). This information doesn't seem to be available in the public API, but I wonder if, developping a custom class that expands TreeView, there could be a way to bind my component to its "visible height".
There maybe a proper way to achieve your goal, but I can suggest the approach similar to tableview's.
orgTree.prefHeightProperty().bind(orgTree.impl_treeItemCountProperty()
.multiply(treeRowHeight)); // treeRowHeight maybe 24.
Note that this is only a workaround where the deprecated and internal use-only method is used, which maybe removed in future releases.
I was looking for way to make TreeView auto-resize to fit its content, e.g. when a node is expanded or collapsed. Here's what I eventually do. Note the Platform.runLater() is required, thus simple property binding would not work here. Also note the cellHeight is the height of a tree node. It's beyond the scope of the question here how to get that.
treeView.expandedItemCountProperty().addListener((obs, oldV, newV) -> {
Platform.runLater(() -> {
treeView.setPrefHeight(newV.intValue() * cellHeight);
});
});
You can use the binding property of the view to the childs height property. By this, whenever the height of the Parent changes, childs height will automatically change ! This is not automatic height adaption, like you want, but a forced adaption, which will help you !
Width property is also available
Basically this is a snippet that will help you :
pane.heightProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> arg0,
Number oldValue, Number newValue) {
table.setPrefHeight(newValue - 200);/*The difference of height between
parent and child to be maintained*/
}
});

Resources