Stylesheet applying in javafx scene builder but not when you run application - javafx

So I have a stylesheet with just one style within at the moment for testing purposes.
CSS code:
.minimizeBtn{
-fx-background-color: rgba(0,0,0,1);
}
Setting the ID of the button. This is in the controller for the stage.
public void initialize(URL url, ResourceBundle rb) {
handler = new DbHandlers();
minimizeBtn1.setId("minimizeBtn");
My buttons in fxml:
<Button fx:id="minimizeBtn1" layoutX="520.0" mnemonicParsing="false"
prefHeight="25.0" prefWidth="12.0" styleClass="minimizeBtn" text="Button" />
<Button layoutX="548.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="maximizeBtn" text="Button" />
<Button layoutX="574.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="closeBtn" text="Button" />
The rest of my fxml file:
<AnchorPane prefHeight="450.0" prefWidth="600.0" style="-fx-background-color: #2A7FFF;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="bownhrmain1.pages.LoginScreenController">
<children>
<AnchorPane prefHeight="450.0" prefWidth="600.0" style="-fx-background-color: #2A7FFF;">
<children>
<Label layoutX="335.0" layoutY="144.0" text="Inventory Systems" textFill="WHITE">
<font>
<Font size="8.0" />
</font>
</Label>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="200.0" layoutY="65.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../images/benx_logo.png" />
</image>
</ImageView>
<Hyperlink layoutX="250.0" layoutY="368.0" text="Forgot Password?" textFill="WHITE" underline="true" />
<JFXButton fx:id="loginBtn" buttonType="RAISED" layoutX="190.0" layoutY="302.0" prefHeight="51.0" prefWidth="221.0" ripplerFill="#2980b9" style="-fx-background-color: #1E5D87;" text="Login" textFill="WHITE" />
<JFXPasswordField fx:id="passwordField" focusColor="#1e5d87" labelFloat="true" layoutX="190.0" layoutY="237.0" prefHeight="34.0" prefWidth="221.0" promptText="Password..." unFocusColor="WHITE" />
<JFXTextField id="text-field" fx:id="usernameField" focusColor="#1e5d87" labelFloat="true" layoutX="190.0" layoutY="176.0" prefHeight="32.0" prefWidth="221.0" promptText="Username/Email..." unFocusColor="WHITE" />
<ImageView fitHeight="20.0" fitWidth="19.0" layoutX="522.0" layoutY="5.0" pickOnBounds="true">
<image>
<Image url="#../images/minimize.png" />
</image>
</ImageView>
<ImageView fitHeight="11.0" fitWidth="12.0" layoutX="554.0" layoutY="9.0" pickOnBounds="true">
<image>
<Image url="#../images/expand-button.png" />
</image>
</ImageView>
<ImageView fitHeight="11.0" fitWidth="11.0" layoutX="581.0" layoutY="9.0" pickOnBounds="true">
<image>
<Image url="#../images/closeWindow.png" />
</image>
</ImageView>
<Button fx:id="minimizeBtn1" layoutX="520.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="minimizeBtn" text="Button" />
<Button layoutX="548.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="maximizeBtn" text="Button" />
<Button layoutX="574.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="12.0" styleClass="closeBtn" text="Button" />
</children>
</AnchorPane>
</children>
</AnchorPane>
The reason i have the styleclass on the buttons is because I have tried both setting the css through class name and ID.
Here's an image of the css working in JavaFx scene builder:
CSS Applying in scene builder.
Here's an imagee of it failing to apply when you run the program:
Not applying

So the answer to this question is to add the following to your start function:
scene.getStylesheets().add(getClass().getResource("pathtostylesheet").toExternalForm());

Related

Error when trying to use setText() method ( java Fx, sceneBuilder )

I am working on a employee management system using java fx and mysql. Basically, when the user click on a row of the tableview, it stock the row data in a employee object, than to String variable. At the same times, an edit scene open. I want the TextField in the new scene to be set up with the previous variable that the row had.
The method displaySelected() is a mouseEvent method, so its in this method that I retrieve row data. Right after retrieving the data, I call switchEdit() method, its only purpose is to open the Edit scene.
*Note that retrieving data from row is working just fine, I print it to be sure.
Right after opening the new scene, I use setText method to set the data in the TextField, thats where the error occurs. I get :
Cannot invoke "javafx.scene.control.TextField.setText(String)" because "this.idEdit" is null
I doubled check, I got all the necessary #FXML import and I checked previous similar question but it didn't help or maybe I didn't know how to implement it in my own code...
I even tried to setText directly in switchEdit method, but I get the same mistakes, and I think the problem may occur because im using one controller on two scene?
I honestly did my best before asking my question, thanks for your help and Ill take any advice on the way I typed my answer so next time Ill be better at helping you to help me.
Board.java class control the dashboard scene and the edit scene :
public class Board {
#FXML
private TableView<employeeList> employee;
#FXML
private TableColumn<employeeList, String> firstColumn;
#FXML
private TableColumn<employeeList, String> genderColumn;
#FXML
private TableColumn<employeeList, String> idColumn;
#FXML
private TableColumn<employeeList, String> lastColumn;
#FXML
private TableColumn<employeeList, String> yoeColumn;
#FXML
private Button refresh;
public ObservableList<employeeList> data = FXCollections.observableArrayList();
public void refreshTable() {
//CLEAN TABLEVIEW BEFORE REFRESH
employee.getItems().clear();
try {
String query = "select * from employee";
DataBase connectLive = new DataBase();
Connection connectDb = connectLive.getConnection();
Statement st;
ResultSet rs;
st = connectDb.createStatement();
rs = st.executeQuery(query);
employeeList emp;
while (rs.next()) {
emp = new employeeList(rs.getInt("id"), rs.getString("firstname"), rs.getString("lastname"), rs.getString("gender"), rs.getString("yoe"));
data.add(emp);
}
connectDb.close();
} catch (Exception e) {
e.printStackTrace();
}
idColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("id"));
firstColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("firstname"));
lastColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("lastname"));
genderColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("gender"));
yoeColumn.setCellValueFactory(new PropertyValueFactory<employeeList, String>("yoe"));
employee.setItems(data);
}
public void displaySelected(MouseEvent event) throws IOException {
employeeList emp = employee.getSelectionModel().getSelectedItem();
if (emp ==null) {
System.out.println("Ya R");
}
else {
String f = emp.getFirstname();
String l = emp.getLastname();
String i = String.valueOf(emp.getId());
String g = emp.getGender();
String y = emp.getYoe();
switchEdit();
idEdit.setText(i);
firstnameEdit.setText(f);
lastnameEdit.setText(l);
genderEdit.setText(g);
yoeEdit.setText(y);
// settEdit(f,l,i,g,y);
}
}
//EDIT
#FXML
private Button exitEdit;
#FXML
private Button buttonfinish;
#FXML
private TextField firstnameEdit;
#FXML
private TextField genderEdit;
#FXML
private TextField idEdit;
#FXML
private TextField lastnameEdit;
#FXML
private TextField yoeEdit;
#FXML
private AnchorPane paneEdit;
public void exitEdit(ActionEvent e) {
Stage stage;
stage = (Stage) paneEdit.getScene().getWindow();
stage.close();
}
public void exit() {
Stage stage;
stage = (Stage) paneEdit.getScene().getWindow();
stage.close();
}
public void switchEdit() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/application/Edit.fxml"));
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Stage primaryStage = new Stage();
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
}
public void switchE(ActionEvent e) throws IOException {
switchEdit();
}
}
fxml code for the board:
<?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.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Board">
<children>
<AnchorPane fx:id="pane2" prefHeight="860.0" prefWidth="920.0" style="-fx-background-color: #1B2430;">
<children>
<VBox layoutX="60.0" layoutY="25.0" prefHeight="797.0" prefWidth="777.0" style="-fx-background-color: #EEEEEE; -fx-background-radius: 30;">
<children>
<AnchorPane prefHeight="28.0" prefWidth="777.0">
<children>
<Button fx:id="exitButton" layoutX="734.0" layoutY="20.0" mnemonicParsing="false" onAction="#exit" prefHeight="20.0" prefWidth="20.0" style="-fx-background-radius: 100; -fx-background-color: #7b3733;" text="X" textFill="#eeeeee">
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color>
</DropShadow>
</effect>
<font>
<Font name="Consolas" size="12.0" />
</font>
</Button>
</children>
</AnchorPane>
<HBox alignment="CENTER" prefHeight="119.0" prefWidth="737.0" style="-fx-background-color: #336e7b; -fx-background-radius: 30;">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" text="Dash Board" textFill="WHITE">
<font>
<Font name="Calibri Light" size="24.0" />
</font>
<HBox.margin>
<Insets right="30.0" />
</HBox.margin>
</Label>
</children>
<VBox.margin>
<Insets left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</HBox>
<HBox prefHeight="42.0" prefWidth="581.0">
<children>
<BorderPane prefHeight="200.0" prefWidth="200.0" />
<BorderPane prefHeight="38.0" prefWidth="1026.0">
<right>
<TextField prefHeight="25.0" prefWidth="219.0" promptText="🔎 Search keywords" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets right="20.0" />
</BorderPane.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
</right>
<left>
<Button fx:id="refresh" mnemonicParsing="false" onAction="#refreshTable" prefHeight="25.0" prefWidth="82.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;" text="🗘" textFill="WHITE" textOverrun="CLIP" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets bottom="2.0" left="20.0" />
</BorderPane.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</Button>
</left>
</BorderPane>
</children>
</HBox>
<BorderPane prefHeight="364.0" prefWidth="777.0">
<center>
<TableView fx:id="employee" onMouseClicked="#displaySelected" prefHeight="345.0" prefWidth="737.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="idColumn" prefWidth="84.0" text="ID" />
<TableColumn fx:id="firstColumn" prefWidth="163.0" text="First Name" />
<TableColumn fx:id="lastColumn" prefWidth="163.0" text="Last Name" />
<TableColumn fx:id="genderColumn" prefWidth="163.0" text="Gender" />
<TableColumn fx:id="yoeColumn" prefWidth="163.0" text="Year Of Experience" />
</columns>
<BorderPane.margin>
<Insets left="20.0" right="20.0" />
</BorderPane.margin>
</TableView>
</center>
</BorderPane>
<BorderPane prefHeight="60.0" prefWidth="777.0">
<left>
<Button fx:id="addButton" mnemonicParsing="false" onAction="#addButton" prefHeight="25.0" prefWidth="200.0" style="-fx-background-color: #336e7b; -fx-background-radius: 30;" text="Add" textFill="WHITE" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="20.0" right="20.0" />
</BorderPane.margin>
<font>
<Font name="Calibri" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color></DropShadow>
</effect>
</Button>
</left>
<right>
<Button fx:id="deleteButton" mnemonicParsing="false" onAction="#switchD" prefWidth="200.0" style="-fx-background-color: #7b3733; -fx-background-radius: 30;" text="Delete" textFill="WHITE" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="20.0" right="20.0" />
</BorderPane.margin>
<font>
<Font name="Calibri" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color></DropShadow>
</effect>
</Button>
</right>
<center>
<Button fx:id="EditButton" mnemonicParsing="false" onAction="#switchE" prefHeight="25.0" prefWidth="200.0" style="-fx-background-color: #808080; -fx-background-radius: 30;" text="Edit" textFill="WHITE" BorderPane.alignment="CENTER">
<font>
<Font name="Calibri" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color></DropShadow>
</effect>
</Button>
</center>
</BorderPane>
<BorderPane prefHeight="57.0" prefWidth="777.0">
<VBox.margin>
<Insets top="90.0" />
</VBox.margin>
<center>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Copyright © 2022 - Amine Lakhal - Raphael Ducros" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
</children>
</VBox>
</children>
</AnchorPane>
</children>
</AnchorPane>
fxml code for the edit page
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.paint.Color?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:id="paneEdit" prefHeight="775.0" prefWidth="493.0" style="-fx-background-color: #1B2430;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Board">
<children>
<VBox layoutX="65.0" layoutY="30.0" prefHeight="715.0" prefWidth="364.0" style="-fx-background-color: #EEEEEE; -fx-background-radius: 30;">
<children>
<Button fx:id="exitEdit" mnemonicParsing="false" onAction="#exitEdit" prefHeight="20.0" prefWidth="20.0" style="-fx-background-radius: 100; -fx-background-color: #7b3733;" text="X" textFill="#eeeeee">
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color>
</DropShadow>
</effect>
<font>
<Font name="Consolas" size="12.0" />
</font>
<VBox.margin>
<Insets left="328.0" top="13.0" />
</VBox.margin>
</Button>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="359.0" style="-fx-background-color: #336e7b; -fx-background-radius: 30;">
<children>
<ImageView>
<image>
<Image url="#edit.png" />
</image>
</ImageView>
<Label alignment="CENTER" contentDisplay="CENTER" text=" Edit Employee" textFill="WHITE">
<font>
<Font name="Calibri Light" size="24.0" />
</font>
<HBox.margin>
<Insets right="30.0" />
</HBox.margin>
</Label>
</children>
<VBox.margin>
<Insets left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</HBox>
<Label prefHeight="35.0" prefWidth="314.0" text="ID Employe *" textFill="#8a959a">
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
</Label>
<TextField fx:id="idEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<font>
<Font name="Consolas" size="12.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="314.0" text="Last Name" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="10.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="lastnameEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="473.0" text="First Name" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="15.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="firstnameEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="311.0" text="Gender " textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="15.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="genderEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="35.0" prefWidth="318.0" text="Year Of Experience" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" right="20.0" top="15.0" />
</VBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</Label>
<TextField fx:id="yoeEdit" prefHeight="35.0" style="-fx-background-radius: 30; -fx-background-color: #D3D3D3;">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.30000001192092896" />
</color>
</DropShadow>
</effect>
</TextField>
<Label prefHeight="15.0" prefWidth="310.0" text=" Information can be modified later" textFill="#8a959a">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
<font>
<Font name="Calibri" size="11.0" />
</font>
</Label>
<HBox prefHeight="35.0" prefWidth="200.0">
<children>
<CheckBox mnemonicParsing="false" text="All data have been confirmed by manager" textFill="#8a959a">
<HBox.margin>
<Insets left="20.0" top="5.0" />
</HBox.margin>
<font>
<Font name="Calibri Italic" size="14.0" />
</font>
</CheckBox>
</children>
</HBox>
<Button fx:id="buttonfinish" mnemonicParsing="false" prefHeight="47.0" prefWidth="359.0" style="-fx-background-radius: 30; -fx-background-color: #336e7b;" stylesheets="#application.css" text="Finish" textFill="WHITE">
<VBox.margin>
<Insets left="20.0" right="20.0" />
</VBox.margin>
<font>
<Font name="Calibri Light" size="18.0" />
</font>
<effect>
<DropShadow offsetY="5.0">
<color>
<Color opacity="0.5" />
</color>
</DropShadow>
</effect>
</Button>
<Text fill="#8a959a" strokeType="OUTSIDE" strokeWidth="0.0" text="All sensitive data are subject to confidentiality rules in force in the province of Quebec. Any illegal use will be punished by law. 23:788:22|667" wrappingWidth="297.13671875">
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="10.0" />
</VBox.margin>
<font>
<Font name="Calibri" size="11.0" />
</font>
</Text>
</children>
</VBox>
</children>
</AnchorPane>
Don't use the same controller class (application.Board) for different FXML files.
Each time you load the FXML, the loader will create a new instance of the controller class, only initializing the FXML fields associated with the new FXML fields in the new controller instance.
In your case, the idEdit field is initialized in the controller instance created by the FXML Loader invoked in your switchEdit() method. But that instance differs from the instance that called the switchEdit() method.
Instead, create a new class to form the controller for the employee data editor.
Use MVC to share a model or pass parameters to the new controller. In your case, the shared model or parameters to pass would be the id or data of the selected employee.
The Makery JavaFX tutorial provides an illustrative example.

Aligning item of VBox and HBox to different position from other items

I'm designing a dashboard in JavaFX FXML. Here is a screenshot of the dashboard:
Here is FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.farhad.controllers.DashboardMenuController"
spacing="50.0"
id="dashboard-menu"
stylesheets="#../style/dashboard.css">
<HBox alignment="CENTER" spacing="5.0">
<ImageView>
<Image url="#../icons/user.png" />
</ImageView>
<VBox spacing="5.0" alignment="CENTER_LEFT">
<Label id="user-full-name" text="User full name"/>
<Label id="user-login-name" text="User login name"/>
</VBox>
</HBox>
<Separator />
<!-- About User -->
<VBox spacing="20.0">
<HBox>
<ImageView>
<Image url="#../icons/overview.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Overview"/>
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/accounts.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Accounts" />
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/transaction.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Transactions"/>
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/products.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Your Products"/>
<Separator orientation="VERTICAL"/>
</HBox>
</VBox>
<Separator />
<!-- Settings -->
<VBox spacing="20.0">
<HBox>
<ImageView>
<Image url="#../icons/add-account.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Add New Account" />
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/merchant.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Be Merchant"/>
<Separator orientation="VERTICAL"/>
</HBox>
<HBox>
<ImageView>
<Image url="#../icons/settings.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="User Settings" />
<Separator orientation="VERTICAL" />
</HBox>
</VBox>
<!-- Logout -->
<VBox alignment="BOTTOM_CENTER">
<HBox >
<ImageView>
<Image url="#../icons/logout.png" />
</ImageView>
<Label styleClass="dashboard-menu-item-label" text="Logout" />
<Separator orientation="VERTICAL" />
</HBox>
</VBox>
</VBox>
My problem is in the Logout label and a little white vertical separator in each menu item. I want to place the Logout label at the bottom of Parent VBOX and a little white vertical separator to the left of HBOX as other apps.
How can I modify the code so my dashboard looks like this:
Suppose that this dashboard has a Logout label at the bottom of the VBox

Javafx AnchorPane in Maxsized screen always placed top left side

i use scene builder to build a gui for my app and i have this problem:
When stage screen is set to Maximized , AnchorPane is set top left corner and all it's contents inside(VBox etc). i want to cover the full screen or at least be at the center. Im not too familiar with anchorPane alignment and positioning but wit borderpane i didn't have that problem. So here is how my screen looks :
So the anchorpane just is being placed always top left like this.
Here is my fxml code:
<AnchorPane prefHeight="394.0" prefWidth="586.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gr.hua.dit.oop2.gui.Controller2">
<children>
<VBox layoutY="29.0" prefHeight="116.0" prefWidth="344.0" spacing="30.0">
<children>
<TextField fx:id="name" alignment="TOP_CENTER" promptText="Enter your full name" />
<TextField fx:id="age" alignment="BASELINE_CENTER" promptText="Enter your age" />
</children>
</VBox>
<VBox layoutX="425.0" layoutY="54.0" prefHeight="300.0" prefWidth="161.0" spacing="10.0">
<children>
<Label alignment="CENTER_RIGHT" text="Tell us your interests" />
<CheckBox fx:id="museum" layoutX="10.0" layoutY="36.0" mnemonicParsing="false" text="Museums" />
<CheckBox fx:id="sights" layoutX="10.0" layoutY="62.0" mnemonicParsing="false" text="Historical Sights" />
<CheckBox fx:id="night" layoutX="10.0" layoutY="88.0" mnemonicParsing="false" text="Night Life" />
<CheckBox fx:id="forest" layoutX="10.0" layoutY="114.0" mnemonicParsing="false" text="Forests" />
<CheckBox fx:id="lake" mnemonicParsing="false" text="Lakes" />
<CheckBox fx:id="zoo" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" text="Zoo" />
<CheckBox fx:id="sea" layoutX="10.0" layoutY="218.0" mnemonicParsing="false" text="Sea - Beach" />
<CheckBox fx:id="mountain" layoutX="10.0" layoutY="254.0" mnemonicParsing="false" text="Mountains" />
</children>
</VBox>
<Button fx:id="submit" alignment="BOTTOM_CENTER" layoutX="269.0" layoutY="360.0" mnemonicParsing="false" text="Proposal">
<font>
<Font size="15.0" />
</font>
</Button>
<VBox layoutX="10.0" layoutY="174.0" prefHeight="161.0" prefWidth="375.0">
<children>
<TextField fx:id="cities" alignment="CENTER" prefHeight="36.0" prefWidth="334.0" promptText="Enter up to 5 Desired Cities you want to Visit">
<font>
<Font size="10.0" />
</font>
</TextField>
<Label alignment="CENTER" contentDisplay="TOP" prefHeight="18.0" prefWidth="377.0" text="e.g. "Athens gr,Berlin de,New York us" />
</children>
</VBox>
</children>
</AnchorPane>

How can I do dynamic layouts in Javafx?

First of all, excuse me if my English is bad, I don't speak English.
So, I'm working with javafx, when I say "dynamic layouts", I refer to the fact that I have my .fxml already programmed with a default structure which it is so.
menu.fxml view
and its code is this
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import java.lang.String?>
<?import java.net.URL?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefWidth="356.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox prefHeight="243.0" prefWidth="356.0" spacing="2.0" stylesheets="#../styles/Containers.css" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<AnchorPane prefHeight="31.0" prefWidth="356.0">
<children>
<HBox prefHeight="35.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<children>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
<image>
<Image url="#../icons/icons8_Calendar_100px.png" />
</image>
</ImageView>
<Label fx:id="lblFecha" prefHeight="31.0" prefWidth="306.0" stylesheets="#../styles/Strings.css" text="---- ----, -----">
<HBox.margin>
<Insets left="15.0" />
</HBox.margin>
<styleClass>
<String fx:value="h4" />
<String fx:value="principal" />
</styleClass>
</Label>
</children>
</HBox>
</children>
</AnchorPane>
<AnchorPane>
<children>
<HBox prefHeight="35.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../icons/icons8_Clock_100px.png" />
</image>
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</ImageView>
<Label fx:id="lblHora" prefHeight="31.0" prefWidth="306.0" stylesheets="#../styles/Strings.css" text="-- : -- : -- --">
<HBox.margin>
<Insets left="15.0" />
</HBox.margin>
<styleClass>
<String fx:value="h4" />
<String fx:value="principal" />
</styleClass>
</Label>
</children>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
</HBox>
</children>
</AnchorPane>
<AnchorPane fx:id="opcParlking">
<children>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="25.0" onAction="#openParking" prefHeight="35.0" prefWidth="366.0" styleClass="h4" text="Parking" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true" translateX="10.0">
<image>
<Image url="#../icons/icons8_Tollbooth_100px.png" />
</image>
</ImageView>
</graphic>
<font>
<Font name="System Bold" size="12.0" />
</font>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<stylesheets>
<URL value="#../styles/Buttons.css" />
<URL value="#../styles/Strings.css" />
</stylesheets>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane>
<children>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="25.0" prefHeight="35.0" prefWidth="363.0" styleClass="h4" text="Tipo de vehiculos" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true" translateX="10.0">
<image>
<Image url="#../icons/icons8_Traffic_Jam_96px.png" />
</image>
</ImageView>
</graphic>
<font>
<Font name="System Bold" size="12.0" />
</font>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<stylesheets>
<URL value="#../styles/Buttons.css" />
<URL value="#../styles/Strings.css" />
</stylesheets>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane>
<children>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="25.0" prefHeight="35.0" prefWidth="363.0" styleClass="h4" text="Usuarios" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true" translateX="10.0">
<image>
<Image url="#../icons/icons8_User_Groups_100px.png" />
</image>
</ImageView>
</graphic>
<font>
<Font name="System Bold" size="12.0" />
</font>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<stylesheets>
<URL value="#../styles/Buttons.css" />
<URL value="#../styles/Strings.css" />
</stylesheets>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane>
<children>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="25.0" prefHeight="35.0" prefWidth="361.0" styleClass="h4" text="Clientes" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true" translateX="10.0">
<image>
<Image url="#../icons/icons8_Helping_Hand_104px.png" />
</image>
</ImageView>
</graphic>
<font>
<Font name="System Bold" size="12.0" />
</font>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<stylesheets>
<URL value="#../styles/Buttons.css" />
<URL value="#../styles/Strings.css" />
</stylesheets>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane>
<children>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="25.0" prefHeight="35.0" prefWidth="361.0" styleClass="h4" text="Reportes" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true" translateX="10.0">
<image>
<Image url="#../icons/icons8_Combo_Chart_104px.png" />
</image>
</ImageView>
</graphic>
<font>
<Font name="System Bold" size="12.0" />
</font>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<stylesheets>
<URL value="#../styles/Buttons.css" />
<URL value="#../styles/Strings.css" />
</stylesheets>
</JFXButton>
</children>
</AnchorPane>
<AnchorPane>
<children>
<JFXButton alignment="BASELINE_LEFT" graphicTextGap="25.0" prefHeight="29.0" prefWidth="356.0" styleClass="h4" text="Configuraciones" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<graphic>
<ImageView fitHeight="25.0" fitWidth="25.0" pickOnBounds="true" preserveRatio="true" translateX="10.0">
<image>
<Image url="#../icons/icons8_Automation_100px.png" />
</image>
</ImageView>
</graphic>
<font>
<Font name="System Bold" size="12.0" />
</font>
<padding>
<Insets bottom="8.0" top="8.0" />
</padding>
<stylesheets>
<URL value="#../styles/Buttons.css" />
<URL value="#../styles/Strings.css" />
</stylesheets>
</JFXButton>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</AnchorPane>
I want to be able in my execution time to hide any buttons and that the buttons adjust automatically filling the blank space that the hidden button leaves.
I've tried using in the MenuController.java instructions like opcParlking.setVisible(false) but it leave me a blank space. This the picture imagen 2.
How can I fix this?
You will want to assign an fx:id to your VBox as well and then inject it into your controller:
#FXML
private VBox vBox;
Then, in your controller, instead of setting a node visible or not, just remove it from the VBox:
vBox.getChildren().remove(opcParlking);
This will remove the node entirely, instead of just making it invisible. Therefore, you will not have the empty space.

On hover focused blue background color

I am having one strange problem, though I have good knowdedge about css and all. But in javafx I am facing strange situation. When I take cursor away from the MenuItem, the background color goes blue.
Here is my code
<MenuButton id="scMenuButton" fx:id="searchCriteriaMenu" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Search Template" textFill="WHITE" wrapText="true">
<items>
<!-- <MenuItem id="scMenuItem" fx:id="scMenuItem" mnemonicParsing="false" style="-fx-background-color: transparent; -fx-padding: 0; -fx-margin: 0;">
<graphic> -->
<CustomMenuItem id="#searchCustomMenuItem" fx:id="scMenuItem" hideOnClick="false" mnemonicParsing="false" style="-fx-background-color: transparent; -fx-padding: 0; -fx-margin: 0;" styleClass="redBorder">
<content>
<VBox id="searchMbox" fx:id="advancedSearchVbox" prefHeight="406.0" prefWidth="359.0" style="-fx-font-weight: bold;" visible="false">
<children>
<AnchorPane id="searchAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="615.0" prefWidth="200.0" styleClass="noborder">
<children>
<HBox layoutY="76.0" minHeight="168.0" minWidth="345.0" prefHeight="168.0" prefWidth="357.0">
<children>
<ScrollPane hbarPolicy="NEVER" maxHeight="230.0" minWidth="355.0" prefHeight="216.0" prefWidth="355.0" vbarPolicy="AS_NEEDED">
<content>
<VBox fx:id="userSelectedValues" fillWidth="true" layoutY="5.0" maxWidth="355.0">
</VBox>
</content>
</ScrollPane>
</children>
</HBox>
<Group layoutY="-100.0">
<children>
<HBox layoutX="7.0" layoutY="350.0" prefHeight="35.0" prefWidth="348.0">
<children>
<Text layoutX="7.0" layoutY="335.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Template Name">
<HBox.margin>
<Insets right="5.0" top="9.0" />
</HBox.margin>
</Text>
<ComboBox fx:id="templateNameComboBox" layoutX="94.0" layoutY="330.0" prefWidth="248.0" promptText="Select Template Name">
<HBox.margin>
<Insets left="7.0" />
</HBox.margin>
</ComboBox>
</children>
</HBox>
<HBox layoutX="7.0" layoutY="385.0" prefHeight="35.0" prefWidth="348.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Type of template">
<HBox.margin>
<Insets top="9.0" />
</HBox.margin>
</Text>
<ComboBox fx:id="templateTypeComboBox" prefWidth="248.0" promptText="Select Template Type">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Private" />
<String fx:value="Public" />
</FXCollections>
</items>
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</ComboBox>
</children>
</HBox>
<HBox alignment="TOP_CENTER" layoutX="-8.0" layoutY="422.0" prefHeight="100.0" prefWidth="360.0">
<children>
<CheckBox fx:id="defaultTemplate" mnemonicParsing="false" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Set as a default template">
<HBox.margin>
<Insets top="3.0" />
</HBox.margin>
</Text>
</children>
</HBox>
<HBox layoutX="5.0" layoutY="450.0" prefHeight="45.0" prefWidth="352.0">
<children>
<Button mnemonicParsing="false" onAction="#onSaveTemplate" prefHeight="45.0" prefWidth="67.0" styleClass="advancedRedButton" text="Save Template" textAlignment="CENTER" wrapText="true">
<cursor>
<Cursor fx:constant="HAND" />
</cursor></Button>
<Button mnemonicParsing="false" onAction="#onEditTemplate" prefHeight="45.0" prefWidth="67.0" styleClass="advancedRedButton" text="Edit Template" textAlignment="CENTER" wrapText="true">
<HBox.margin>
<Insets left="8.0" />
</HBox.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
<Button mnemonicParsing="false" onAction="#onDeleteTemplate" prefHeight="45.0" prefWidth="67.0" styleClass="advancedRedButton" text="Delete Template" textAlignment="CENTER" wrapText="true">
<HBox.margin>
<Insets left="8.0" />
</HBox.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
<Button mnemonicParsing="false" onAction="#onClearTemplate" prefHeight="45.0" prefWidth="55.0" styleClass="advancedRedButton" text="Clear" textAlignment="CENTER" wrapText="true">
<HBox.margin>
<Insets left="8.0" />
</HBox.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
<Button mnemonicParsing="false" onAction="#advancedSearch" prefHeight="45.0" prefWidth="55.0" styleClass="advancedRedButton" text="Search">
<HBox.margin>
<Insets left="8.0" />
</HBox.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
</children>
</HBox>
</children>
</Group>
<HBox layoutX="5.0" layoutY="5.0" prefHeight="73.0" prefWidth="348.0">
<children>
<GridPane fx:id="gridPane" hgap="3.0" layoutX="6.6" layoutY="60.6" prefHeight="43.0" prefWidth="348.0" vgap="1.0" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="-3.0" AnchorPane.rightAnchor="-10.0" AnchorPane.topAnchor="51.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="132.99999237060547" minWidth="10.0" prefWidth="126.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="102.0" minWidth="10.0" prefWidth="102.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="115.0" minWidth="10.0" prefWidth="86.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="76.0" minWidth="10.0" prefWidth="28.0" />
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="140.0" minHeight="10.0" prefHeight="24.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="275.0" minHeight="10.0" prefHeight="48.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Field" textAlignment="CENTER" wrappingWidth="35.9765625" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<styleClass>
<String fx:value="boldFont" />
<String fx:value="px16Font" />
</styleClass>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Operator" GridPane.columnIndex="1" GridPane.halignment="CENTER">
<styleClass>
<String fx:value="boldFont" />
<String fx:value="px16Font" />
</styleClass>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Value" GridPane.columnIndex="2" GridPane.halignment="CENTER">
<styleClass>
<String fx:value="boldFont" />
<String fx:value="px16Font" />
</styleClass>
</Text>
<ComboBox fx:id="fieldName" prefWidth="150.0" promptText="Select Field" GridPane.rowIndex="1">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="CLSMESSAGEID" />
<String fx:value="PAYMENTTYPE" />
<String fx:value="REQUESTTYPE" />
<String fx:value="VALUEDATE" />
</FXCollections>
</items>
</ComboBox>
<ComboBox fx:id="operator" prefWidth="150.0" promptText="Select Operator" GridPane.columnIndex="1" GridPane.rowIndex="1">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="IN" />
<String fx:value="NOT IN" />
<String fx:value="EQUALS" />
<String fx:value="NOT EQUALS" />
<String fx:value="LESS THAN" />
<String fx:value="LESS THAN EQUALS" />
<String fx:value="GREATER THAN" />
<String fx:value="GREATER THAN EQUALS" />
</FXCollections>
</items>
</ComboBox>
<TextField fx:id="fieldValue" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Button fx:id="addImage" mnemonicParsing="false" onAction="#addOptions" prefHeight="20.0" prefWidth="20.0" styleClass="addImg" GridPane.columnIndex="3" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="5.0" />
</GridPane.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Button>
</children>
</GridPane>
</children>
</HBox>
</children>
</AnchorPane>
</children>
</VBox>
</content>
</CustomMenuItem>
<!-- </graphic>
</MenuItem> -->
</items>
<HBox.margin>
<Insets left="6.0" />
</HBox.margin>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<styleClass>
<String fx:value="redButton" />
<String fx:value="whiteText" />
</styleClass>
</MenuButton>

Resources