JavaFX FXML Align to the left - javafx

I'm trying to align the Text to the left but keep the picture in the center. How can I achieve this? (the image can be change, it's just there for reference)
Here's my code:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<VBox prefHeight="300.0" prefWidth="600.0" alignment="CENTER" fx:controller="controller.LoginController" xmlns:fx="http://javafx.com/fxml/1" spacing="10">
<HBox alignment="TOP_RIGHT" spacing="10">
<Button fx:id="loginBtn" text="Login" onAction="#handleLogin"/>
<Button fx:id="exitBtn" text="Exit" onAction="#handleExit" />
</HBox>
<Label text="STPFX" />
<Label text="Payroll Management System"/>
<ImageView fitHeight="300" fitWidth="300" >
<image><Image url ="#stp.jpg" /></image>
</ImageView>
</VBox>

You should create VBox for the 2 labels with alignment = left and maxWidth.
<VBox prefHeight="300.0" prefWidth="600.0" alignment="CENTER" fx:controller="controller.LoginController" xmlns:fx="http://javafx.com/fxml/1" spacing="10">
<HBox alignment="TOP_RIGHT" spacing="10">
<Button fx:id="loginBtn" text="Login" onAction="#handleLogin"/>
<Button fx:id="exitBtn" text="Exit" onAction="#handleExit" />
</HBox>
<VBox alignment="CENTER_LEFT" maxWidth="1.7976931348623157E308">
<Label text="STPFX" />
<Label text="Payroll Management System"/>
</VBox>
<ImageView fitHeight="300" fitWidth="300" >
<image><Image url ="#stp.jpg" /></image>
</ImageView>
</VBox>

Related

How to make components fit width of the GridPane they're added to?

I am trying to achieve this layout using FXML and GridPane.
what I achieved is this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.geometry.Insets?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane fx:controller="com.mycompany.mavenproject1.PrimaryController" xmlns:fx="http://javafx.com/fxml" alignment="center">
<Label fx:id="label" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.hgrow="always" GridPane.vgrow="always" />
<TextField fx:id="a" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="b" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField fx:id="c" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Button fx:id="solveButton" text="Solve" onAction="#solve" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Button fx:id="clearButton" text="Clear" onAction="#clear" GridPane.columnIndex="0" GridPane.rowIndex="3" />
</GridPane>
Question: How do I make children actually fit the GridPane and why those textfields are not of the same size?
Another alternate way to achieve this is to just relay on the GridPane constraint features.
You can add Column/Row constraints and set the fillHeight/fillWidth to true and hgrow/vgrow policies to ALWAYS. Also set the desired nodes maxWidth/maxHeight to Infinity to auto stretch.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane xmlns:fx="http://javafx.com/fxml" alignment="center">
<Label fx:id="label" text="Number Format Error !" maxWidth="Infinity" alignment="CENTER" GridPane.columnSpan="3"/>
<TextField fx:id="a" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="b" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField fx:id="c" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<Button fx:id="solveButton" text="Solve" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnSpan="3" GridPane.rowIndex="2" />
<Button fx:id="clearButton" text="Clear" maxWidth="Infinity" maxHeight="Infinity" GridPane.columnSpan="3" GridPane.rowIndex="3" />
<columnConstraints>
<ColumnConstraints fillWidth="true" hgrow="ALWAYS"/>
<ColumnConstraints fillWidth="true" hgrow="ALWAYS"/>
<ColumnConstraints fillWidth="true" hgrow="ALWAYS"/>
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="ALWAYS"/>
<RowConstraints vgrow="NEVER"/>
<RowConstraints fillHeight="true" vgrow="ALWAYS"/>
<RowConstraints fillHeight="true" vgrow="ALWAYS"/>
</rowConstraints>
</GridPane>
How do I make children actually fit the GridPane [...]?
You can wrap a child node in an AnchorPane and set the anchors to zero.
[...] and why those textfields are not of the same size?
I don't know but you can use ColumnConstraints and set each column width to 33.33 %.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.mycompany.mavenproject1.PrimaryController">
<columnConstraints>
<ColumnConstraints percentWidth="33.33"/>
<ColumnConstraints percentWidth="33.33"/>
<ColumnConstraints percentWidth="33.33"/>
</columnConstraints>
<AnchorPane GridPane.columnSpan="3" GridPane.hgrow="always" GridPane.vgrow="always">
<children>
<Label fx:id="label" alignment="CENTER" style="-fx-background-color: tomato;" text="Number format error!"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
<TextField fx:id="a" GridPane.columnIndex="0" GridPane.rowIndex="1"/>
<TextField fx:id="b" GridPane.columnIndex="1" GridPane.rowIndex="1"/>
<TextField fx:id="c" GridPane.columnIndex="2" GridPane.rowIndex="1"/>
<AnchorPane GridPane.columnSpan="3" GridPane.rowIndex="2">
<Button fx:id="solveButton" onAction="#solve" text="_Solve" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</AnchorPane>
<AnchorPane GridPane.columnSpan="3" GridPane.rowIndex="3">
<Button fx:id="clearButton" onAction="#clear" text="_Clear" AnchorPane.bottomAnchor="0.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</AnchorPane>
</GridPane>

How to create a checkbox in a button? (javafx)

How to create a checkbox in a button? If it is possible to create it through the fxml file it would be better for me, but also another option would be good.
I tried to put the CheckBox tag inside the Button tag. But it didn't work
<Button text="Write All" styleClass="smallSideButton" style="-fx-pref-width: 120px;" GridPane.rowIndex="2" GridPane.columnIndex="7">
<CheckBox/>
</Button>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.layout.StackPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button mnemonicParsing="false" text="Button">
<graphic>
<CheckBox mnemonicParsing="false" text="CheckBox" />
</graphic>
</Button>
</children>
</StackPane>

How to fix this Unresolved import error when trying to open scene builder

getting this error when i try to open scene builder:
java.lang.ClassNotFoundException: Unresolved import
javafx.fxml.LoadException:
/C:/Users/Mo/everett-project/src/sample/sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import ManufacturerPage.manuController?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.121" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Pane prefHeight="550.0" prefWidth="765.0">
<children>
<Button layoutX="46.0" layoutY="103.0" mnemonicParsing="false" prefHeight="160.0" prefWidth="257.0" text="Inventory" />
<Button layoutX="469.0" layoutY="103.0" mnemonicParsing="false" prefHeight="160.0" prefWidth="257.0" text="Transactions" />
<Button layoutX="46.0" layoutY="330.0" mnemonicParsing="false" prefHeight="160.0" prefWidth="257.0" text="Transaction Types" />
<Button layoutX="469.0" layoutY="330.0" mnemonicParsing="false" prefHeight="160.0" prefWidth="257.0" text="Manufacturer Information" />
<Label layoutX="269.0" layoutY="55.0" text="Everett POS AND Inventory System" />
</children>
</Pane>
</children>
</GridPane>

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")
}

ButtonBar changes position when Stage is resizable

I have a BoarderPane (decoration: UTILITY) where a ButtonBar with two buttons is put to BOTTOM:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pw.yaps.client.jfx.view.SettingsViewController">
<bottom>
<ButtonBar BorderPane.alignment="CENTER">
<buttons>
<Button disable="true" mnemonicParsing="false" text="OK" />
<Button cancelButton="true" mnemonicParsing="false" onAction="#handleCancelAction" text="Cancel" />
</buttons>
<padding>
<Insets bottom="12.0" right="12.0" />
</padding>
</ButtonBar>
</bottom>
</BorderPane>
When the Stage is NOT resizable the layout looks like that:
but when I set it resizable it looks different:
How can this behavior be avoided?

Resources