JavaFx - How to use all vertical height (width) in the HBox with labels - javafx

I need to design a java application using javaFx. In the fxml there is two hBoxes; one filled with text fields and one filled with labels. Every text fields and label hGrow set to ALWAYS but text fields do grow horizontaly but labels wont.
Note that I know I can set a Pref Width to 999999 on all of them and they will behave as I want, but there has to be a more legit way.
Here is the fxml:
<VBox prefHeight="375.0" prefWidth="640.0">
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<TextField promptText="ichi" HBox.hgrow="ALWAYS" />
<TextField promptText="ni " HBox.hgrow="ALWAYS" />
<TextField promptText="san " HBox.hgrow="ALWAYS" />
</HBox>
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<Label text="eins" HBox.hgrow="ALWAYS" />
<Label text="zwei" HBox.hgrow="ALWAYS" />
<Label text="drei" HBox.hgrow="ALWAYS" />
</HBox>
</VBox>
As you can see all the hgrows set to ALYAWS but the results are like this:
Trying to fit all labels in to HBox with equal width distrubition. I figured a way but it is cheeky

Key Code:
Max Width = MAX_VALUE
Hgrow = ALWAYS
Full code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<children>
<TextField maxWidth="1.7976931348623157E308" promptText="ichi" HBox.hgrow="ALWAYS" />
<TextField maxWidth="1.7976931348623157E308" promptText="ni" HBox.hgrow="ALWAYS" />
<TextField maxWidth="1.7976931348623157E308" promptText="san" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<HBox VBox.vgrow="ALWAYS">
<children>
<Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="eins" HBox.hgrow="ALWAYS" />
<Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="zwei" HBox.hgrow="ALWAYS" />
<Label alignment="CENTER" maxWidth="1.7976931348623157E308" text="drei" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
Output:

Related

cannot place button on bottom right corner

In scene builder preview button is on exact spot i.e bottom right corner without any gap but when i run my application there is gap.
Button is on a pane and pane is placed on top of anchor pane.
How can i fix this issue?
same thing happening with other components when i placed them on far right or far bottom.
Please examin the sample. Pay attention to min_width, min_height, pref_width, pref_height, max_width, max_height, vgrow, hgrow attributues in SceneBuiler.
The main concept is that type of pane you use sholud give you desired layout. It should not be hardcoded.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox style="-fx-background-color: yellow;">
<children>
<StackPane VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
<StackPane VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
<StackPane layoutX="10.0" layoutY="210.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
<StackPane layoutX="10.0" layoutY="276.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</StackPane>
<StackPane layoutX="10.0" layoutY="310.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
</children>
</VBox>
<VBox style="-fx-background-color: red;" HBox.hgrow="ALWAYS">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0" style="-fx-background-color: green;" VBox.vgrow="ALWAYS">
<children>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" style="-fx-background-color: purple;">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Label" />
<TextField GridPane.columnIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Label" GridPane.rowIndex="1" />
<Label text="Label" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</StackPane>
<HBox alignment="TOP_RIGHT">
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</HBox>
</children>
</VBox>
</children>
</HBox>
Have you put constraints in the layout section of the button? It could be related to that. Check layout x and layout of the button and the pref size x and y of AnchorPane, if that is same, the button must be on the left-bottom corner of the pane.

Scaling/Aligning Elements in a scene when the window is being resized

So I've been searching the internet for about 2 hours now without getting anywhere. I've seen multiple posts already regarding this issue and I tried some of the solutions provided but none seemed to work... or I was just too incompetent to implement them properly...
Anyways, how can I make it so that the elements/controls adjust when the user maximizes the window?
Here's the whole FXML-File:
<GridPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="percolationapplication.controllers.Percolation">
<!-- CUSTOM WINDOW CONTROLS -->
<ToolBar fx:id="toolbar" onMousePressed="#moveableWindowOnMousePressed" onMouseDragged="#moveableWindowOnMouseDragged" prefHeight="25" minHeight="25" maxHeight="25" GridPane.columnIndex="0" GridPane.rowIndex="0">
<ImageView fitWidth="15" fitHeight="15" smooth="true">
<image>
<Image url="/resources/icon.png"/>
</image>
</ImageView>
<Text styleClass="customWindowTitle" text="Percolation Simulation"/>
<Pane HBox.hgrow="ALWAYS" />
<Button fx:id="applicationMinimize" onAction="#minimizeWindow" text="_" styleClass="customWindowButtons"/>
<Button fx:id="applicationMaximize" onAction="#maximizeWindow" text="O" styleClass="customWindowButtons"/>
<Button fx:id="applicationClose" onAction="#closeWindow" text="X" styleClass="customWindowButtons"/>
</ToolBar>
<GridPane hgap="10" GridPane.columnIndex="0" GridPane.rowIndex="1">
<!-- CANVAS -->
<Canvas fx:id="canvas" height="700" width="1100" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<!-- CONTROLS -->
<GridPane hgap="10" vgap="10" GridPane.columnIndex="1" GridPane.rowIndex="0">
<Label text="Number Of Sites:" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<TextField fx:id="quantityInput" text="25" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<Label text="Probability:" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="probabilityInput" text="0.6" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<Label text="Algorithms:" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<HBox spacing="10" GridPane.columnIndex="1" GridPane.rowIndex="2">
<fx:define>
<ToggleGroup fx:id="algs"/>
</fx:define>
<RadioButton fx:id="algNormal" text="Normal" selected="true" toggleGroup="$algs"/>
<RadioButton fx:id="algStack" text="Stack" toggleGroup="$algs"/>
</HBox>
<Label text="System Percolates:" GridPane.columnIndex="0" GridPane.rowIndex="3"/>
<Label fx:id="outputPerc" GridPane.columnIndex="1" GridPane.rowIndex="3"/>
<Button fx:id="perc" onAction="#handlePerc" text="Go" GridPane.columnIndex="0" GridPane.rowIndex="4"/>
</GridPane>
<padding>
<Insets top="10" right="10" bottom="10" left="10"/>
</padding>
</GridPane>
<stylesheets>
<URL value="#/resources/style.css"/>
</stylesheets>
</GridPane>
Here's the window in normal sized mode:
Here's the window in maximized mode. All elements should "grow" to the right and only the canvas should reach the bottom:
You can try this template. You may need to set the height on your ToolBar. You also might need to set your min, max and preferred width to the same number on your right Panel
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane style="-fx-background-color: green;">
<children>
<Label alignment="CENTER" text="Remove this label and put your toolbar here" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: blue;" HBox.hgrow="ALWAYS">
<children>
<Label alignment="CENTER" layoutX="58.0" layoutY="50.0" text="Remove this label and put your picture here" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane maxWidth="200.0" minWidth="200.0" prefWidth="200.0" style="-fx-background-color: yellow;">
<children>
<Label alignment="CENTER" layoutY="40.0" text="Remove this label and put your right panel here" textAlignment="CENTER" wrapText="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</children>
</HBox>
</children>
</VBox>

overlapping of button when size of window increases - javafx - fxml

I have an issue with buttons size. When I increase the size of my window,
The buttons overlap each other. I have tried many thnigs to avoid that, for example wrap each button in an anchorpane, without success.
Here is the fxml code used for the button "devis" for example:
<AnchorPane layoutX="5.0" layoutY="71.0" maxHeight="61.25" prefHeight="58.0" prefWidth="206.0" AnchorPane.bottomAnchor="116.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="71.0">
<children>
<Button fx:id="devis" maxHeight="65.0" mnemonicParsing="false" onAction="#selectViewDevis" prefHeight="65.0" prefWidth="206.0" text="Devis" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font size="14.0" />
</font>
</Button>
</children>
</AnchorPane>
The 2 images of my window before and after increasing
Before increasing
After increasing
Thanks for your help,
Aaron
Try using a 'VBox'.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.35451505016722407" layoutX="189.0" layoutY="167.0" prefHeight="160.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<VBox layoutX="24.0" layoutY="14.0" minWidth="200.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
</children>
</StackPane>
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button" />
</children>
</VBox>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
</items>
</SplitPane>
</children>
</AnchorPane>

Javafx background image using scene builder

I am working on a chatbot project in java and for the GUI I am using JavaFX, and IDE eclipse oxygen and scene builder 8.4.1.
I am having a problem adding a background image to a text area. here is a screen shot of what I did and it just shows nothing(not even an error).
following is the fxml code generated by scene builder:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<top>
<AnchorPane prefHeight="56.0" prefWidth="600.0" style="-fx-background-color: blue;" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="51.0" fitWidth="79.0" layoutX="23.0" layoutY="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../Downloads/background.jpg" />
</image></ImageView>
<Label layoutX="118.0" layoutY="36.0" text="Ibot" />
</children>
</AnchorPane>
</top>
<center>
<TextArea editable="false" prefHeight="200.0" prefWidth="200.0" style="-fx-background-image: url("../../../Downloads/background.jpg"); -fx-background-repeat: stretch;" BorderPane.alignment="CENTER" />
</center>
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<TextField prefHeight="25.0" prefWidth="476.0" promptText="Type in here" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets />
</HBox.margin>
</TextField>
<Region prefHeight="25.0" prefWidth="60.0" />
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="57.0" text="Send">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
</children>
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
<padding>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
</padding>
</HBox>
</bottom>
</BorderPane>
It's probably not working because as specify in JavaFX CSS Reference Guide the TextArea is a association of two substructure: a ScrollPane and a Region. You have to change the style of those.
In CSS this should work:
#text-area-id .content{
-fx-background-image : url("the_path_to_your_image")
/* others properties */
}
Edit to answer comment:
For a button you just need to set the property -fx-background-image:
#your-button{
-fx-background-image : url("the_path_to_your_image")
}

JavaFX- scaling the inner elements of a Pane

When I increase the window, the inner elements stay at the same size.
I want that when I increase the window, that the elements also get larger/scale
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane cacheHint="SCALE_AND_ROTATE" focusTraversable="true" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TableView fx:id="finalTable" layoutX="27.0" layoutY="358.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="190.0" prefWidth="766.0" />
<Label layoutX="27.0" layoutY="21.0" prefHeight="25.0" prefWidth="149.0" text=" Quell-Datei" />
<TableView fx:id="sourceTable" editable="true" layoutX="27.0" layoutY="50.0" maxHeight="900.0" maxWidth="900.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="190.0" prefWidth="766.0" />
<Label layoutX="27.0" layoutY="329.0" prefHeight="25.0" prefWidth="149.0" text=" Konvertierte-Datei" />
<Button fx:id="linkBtn" layoutX="313.0" layoutY="282.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#linkAction" prefHeight="30.0" prefWidth="90.0" text="Verbinden" />
<Button fx:id="splitBtn" layoutX="437.0" layoutY="282.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#splitAction" prefHeight="30.0" prefWidth="90.0" text="Trennen" />
</children>
</AnchorPane>
Im working with SceneBuilder 2.0, and I have also tried to "anchor" a button
(see here: http://i.imgur.com/GZyL5xC.png)
...but the scaling is completely wrong (see here: http://i.imgur.com/hmMi1p3.png)
I searched the whole internet for an answer, but I found nothing that could help.
Well your layout is typically a VBox layout. If your Windows isn't at a fixed size this will be a good solution, because your Buttons stay at the same height and in the center of your window. The TableViews grow and shrink as you resize the window. Like you want.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label prefHeight="25.0" prefWidth="149.0" text=" Quell-Datei" VBox.vgrow="NEVER" />
<TableView fx:id="sourceTable" editable="true" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
<Label />
<HBox alignment="CENTER" prefHeight="61.0" prefWidth="766.0" spacing="20.0" VBox.vgrow="NEVER">
<children>
<Button fx:id="linkBtn" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#linkAction" prefHeight="30.0" prefWidth="90.0" text="Verbinden" HBox.hgrow="NEVER" />
<Button fx:id="splitBtn" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#splitAction" prefHeight="30.0" prefWidth="90.0" text="Trennen" HBox.hgrow="NEVER" />
</children>
</HBox>
<Label prefHeight="25.0" prefWidth="149.0" text=" Konvertierte-Datei" VBox.vgrow="NEVER" />
<TableView fx:id="finalTable" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
</children>
<padding>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</padding>
</VBox>

Resources