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" />
Related
I have been struggling with JavaFX for 2 days now, I have a scene I ought to display on my screen,but I have a problem, when I run the below code in SceneBuilder the code seems perfect everything seems in place, even with the Preview Window option it works but the moment I run my code an issue pops up, the stage seems to be 5+ more bigger in width than the scene, hence I get a layout which has a sort of white area on the right hand side, here is my code
the first Image is showing Layout Results when I run the code, the Asphalt layer is my scene, you can see the white layer on the far right end of my layout and down part but When tested in Scene Builder in the second Image all seems ok, my asphalt layer has filled up everything.
<AnchorPane minHeight="600.0" minWidth="1000.0"
xmlns="http://javafx.com/javafx/8.0.40"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainViewController">
<children>
<StackPane minHeight="600.0" minWidth="1000.0">
<children>
<BorderPane minHeight="600.0" minWidth="1000.0" style="-fx-background-color: #ecf0f1;">
</BorderPane>
</children>
</StackPane>
and my java Code for Inflating the fxml file
Stage primaryStage = new Stage();
primaryStage.setTitle("");
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/mainWindow.fxml"));
Region root = (Region) loader.load();
Scene scene = new Scene(root);
primaryStage.setTitle("");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.centerOnScreen();
primaryStage.show();
primaryStage.setScene(scene);
primaryStage.toFront();
// Set minimum size based on client area's minimum sizes
// Set minimum size based on client area's minimum sizes
Stage loginStage = (Stage) (this.scene.getWindow());
primaryStage.setMinWidth(primaryStage.getWidth());
primaryStage.setMinHeight(primaryStage.getHeight());
primaryStage.show();
loginStage.close();
I don't see anything really wrong in this code, but setting the min size of the stage could be causing this problem, it's useless anyway because the stage uses the size of the scene. If this doesn't work try setting the max size for the Anchorpane or the size in the constructor of the scene in addition to the first solution.
(Btw, I wouldn't use Region for the root container, Parent or the container name are the more typical solutions)
Edit: Where's the code pf the controller? Does it do anything?
it seems the AnchorPane was the problem, I do not know why but after removing it and setting the child StackPane as parent all was sorted.
It's a bug for non-resizable stages on Windows -
see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8089008
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.
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).
I'm creating a 2x2 grid which will contain components inside of them. They will look like the following image:
I'm unsure what the proper layout should be for this. The problem is the bottom right isn't really aligned... so I'm guessing a grid layout won't work (or can it?). I figured I'd do a BorderPane and have the top contain the top two panes, and the bottom contain the bottom two, then do another BorderPane in those and go from there.
1) Is this a good way to approach this (border layout ignoring the center), or is there a better layout?
Next: The blue box will resize based on how large the full screen is. The problem I run into is that after growing large, when it's shrunk, the black boxes (which should always stay the same dimensions, excluding the ones that touch the blue sides which should resize)
2) Is there a way to make the black boxes adjust to the size of the blue box?
By this I mean: If the blue box grows vertically, the top right pane will have to extend vertically as well. Likewise, it also will have to shrink if the blue one does. Can this be done by binding both heights? Since the bottom rectangles won't expand, is there a way to bind it so if the height of the entire pane is x then the bound height that JavaFX will calculate for me will be x - bottomFixedHeight, yet also will not go smaller than lets say 128 (as an arbitrary example)?
Note: I'm not familiar with multiple bindings, I'm trying to google it though maybe an example could help.
Background
IMHO, BorderPane is the best Layout you can use for the above stated scenario.
The Javadoc for BorderPane says,
The top and bottom children will be resized to their preferred heights and extend the width of the border pane. The left and right children will be resized to their preferred widths and extend the length between the top and bottom nodes. And the center node will be resized to fill the available space in the middle.
So using TOP and BOTTOM is not a suitable scenario. I would suggest to go with CENTER and BOTTOM.
SOLUTION
Now that we have decided to use BorderPane, let us decide the Layout we can use for filling the four space.
As we can see in the above picture, we can use the CENTER and RIGHT of the BorderPane to create the upper part. Let us add VBox to center and right space of the borderpane. The VBox on the right side must have a PREF_WIDTH and MAX_WIDTH specified so as to make sure it doesn't exceed a supplied WIDTH. If you want to have a MIN_WIDTH, you can supply that as well.
For the BOTTOM, we can use a HBox, then add two VBox to it. For the first VBox, as we want it to exceed in Width, when re-sized, we will set the HBox.hgrow="ALWAYS". For the bottom right VBox, we will again set the width as we did for the top-right VBox and the HBox.hgrow="NEVER".
Note : You can replace VBox with your preferred layout. PREF_WIDTH and PREF_HEIGHT is used for the initial WIDTH and HEIGHT
I have created a sample using scenebuilder and the output is, as shown
FXML
<BorderPane minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<bottom>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<VBox prefHeight="100.0" prefWidth="100.0" style="-fx-border-color: green;" HBox.hgrow="ALWAYS" />
<VBox minWidth="70.0" prefHeight="200.0" prefWidth="70.0" style="-fx-border-color: black;" HBox.hgrow="NEVER" />
</children>
</HBox>
</bottom>
<right>
<VBox minWidth="100.0" prefHeight="200.0" prefWidth="100.0" style="-fx-border-color: red;" BorderPane.alignment="CENTER" />
</right>
<center>
<VBox prefHeight="200.0" prefWidth="100.0" style="-fx-border-color: blue;" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Authors Note : Though I would not consider binding to be important here, you can anyway use it if you desire for better control.
I have a ploblem with resizing of ImageView in StackPane:
If StackPane is root container and I bind image's height and width to StackPane, everything is fine.
<StackPane id="StackPane" fx:id="stack" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: lightgreen;" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication8.FXMLController">
<children>
<ImageView fx:id="image" fitHeight="100.0" fitWidth="300.0" pickOnBounds="true" preserveRatio="true" />
</children>
<stylesheets>
<URL value="#fxml.css" />
</stylesheets>
</StackPane>
But if I place such stack pane in, for example, grid, than image doesnt resize properly.
As I read, the problem that ImageView is non-Resizable. Is there any ways to make it resizable? Or could you give some advise how to resize ImageView?
It sounds like you are trying to create a background image for the StackPane. In which case applying the image via CSS may be a better route to use than an ImageView.
.stack-pane {
-fx-background-image: url("flower.png");
-fx-background-size: cover;
}
See the background images section of the Region specification in the JavaFX CSS reference guide.
Making ImageView itself resizable is an unresolved major issue. The issue links to code for a simple workaround - a resizable ImagePane wrapping an Image to fill the resizable pane.