"no injectable field found in fxml controller class for the id" - javafx

SceneBuilder is not injecting fields from controller class to FXML despite being annotated by #FXML. Instead when I enter the ID in fx:id on Scene Builder the error message ""no injectable field found in fxml controller class for the id" is instead shown.
I have managed to get it working in the past, where the ID shows a drop down menu and you simply select the appropriate field; however I'm unsure why it has stopped working.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="#../../common/gui/common_style.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="vehicles.logic.addVehicle">
<children>
<Text fill="WHITE" layoutX="304.0" layoutY="263.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Registration Number">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="305.0" layoutY="162.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Model">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="307.0" layoutY="215.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Make">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="306.0" layoutY="314.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Engine Size">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="307.0" layoutY="363.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Fuel Type">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="308.0" layoutY="415.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Colour">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="306.0" layoutY="472.0" strokeType="OUTSIDE" strokeWidth="0.0" text="MOT Renewal Date">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="307.0" layoutY="536.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Last Service Date">
<font>
<Font size="19.0" />
</font></Text>
<Text fill="WHITE" layoutX="309.0" layoutY="597.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Current Mileage">
<font>
<Font size="19.0" />
</font></Text>
<TextField fx:id="model" layoutX="538.0" layoutY="142.0" prefHeight="25.0" prefWidth="170.0" />
<TextField fx:id="make" layoutX="539.0" layoutY="195.0" prefHeight="25.0" prefWidth="170.0" />
<TextField fx:id="regnum" layoutX="540.0" layoutY="244.0" prefHeight="25.0" prefWidth="170.0" />
<TextField fx:id="engine" layoutX="539.0" layoutY="295.0" prefHeight="25.0" prefWidth="170.0" />
<TextField fx:id="fueltype" layoutX="540.0" layoutY="344.0" prefHeight="25.0" prefWidth="170.0" />
<TextField fx:id="colour" layoutX="540.0" layoutY="396.0" prefHeight="25.0" prefWidth="170.0" />
<TextField fx:id="mileage" layoutX="540.0" layoutY="577.0" prefHeight="25.0" prefWidth="170.0" />
<DatePicker fx:id="motrenewal" layoutX="539.0" layoutY="453.0" />
<DatePicker fx:id="servicedate" layoutX="541.0" layoutY="515.0" />
<Button fx:id="add" layoutX="927.0" layoutY="604.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="71.0" text="Add" />
<Button fx:id="back" layoutX="820.0" layoutY="604.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="71.0" text="Back" />
<Text fill="WHITE" layoutX="527.0" layoutY="90.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Add Vehicle">
<font>
<Font size="43.0" />
</font>
</Text>
</children>
</AnchorPane>
Controller:
package vehicles.logic;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* #author ugonw
*/
public class addVehicle implements Initializable {
#FXML
private TextField model;
#FXML
private TextField make;
#FXML
private TextField regnum;
#FXML
private TextField engine;
#FXML
private TextField fueltype;
#FXML
private TextField colour;
#FXML
private TextField mileage;
#FXML
private DatePicker motrenewal;
#FXML
private DatePicker servicedate;
#FXML
private Button add;
#FXML
private Button back;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

a workaround is to open the xml file in your IDE and manually adding the id name to that file. So if Scenebuilder hasen't done this for you and the object concerns a textfield for example, then the tag will look like this.
<TextField />
You should add the fx:id="your_id_name" to it like this:
<TextField fx:id="userName" />
It should work like a charm after saving the xml file. Hope this helps.

Related

javafx fxml - How to make Subscene the same area as the GridPane cell?

I have a program that will have a 2D section on the right, and a 3D section on the left. I use a GridPane to have all of the buttons and other stuff laid out, and then the 3D SubScene takes up the entire first column of the GridPane.
However, I am only able to set the size of the SubScene using hard-coded values, but I would like the SubScene to take up the entire width and height of the GridPane cells that it is inside of. Is there a way to do this?
Note: The GridPane's first column (where the subscene is) changes size when the window is resized, hence why I cannot just hard code the values.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.SubScene?>
<?import javafx.scene.Group?>
<?import javafx.scene.shape.Box?>
<?import javafx.scene.PerspectiveCamera?>
<BorderPane fx:controller="Controller"
xmlns:fx="http://javafx.com/fxml" styleClass="root">
<top>
<MenuBar>
<menus>
<Menu text="File">
<items>
<MenuItem text="New"
onAction="#handleNewSimulationAction" />
<MenuItem text="Save"
onAction="#handleSaveSimulationAction" />
<MenuItem text="Load"
onAction="#handleLoadSimulationAction" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<GridPane hgap="10" vgap="10">
<rowConstraints>
<RowConstraints vgrow="NEVER" />
<RowConstraints vgrow="ALWAYS" />
</rowConstraints>
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" />
</columnConstraints>
<gridLinesVisible>true</gridLinesVisible>
<SubScene fx:id="subscene" width="50" height="50"
GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.rowSpan="6">
<root>
<Region />
</root>
</SubScene>
<Button text="Add" GridPane.columnIndex="1"
GridPane.rowIndex="0" onAction="#handleAddAction" />
<Button text="Edit" GridPane.columnIndex="2"
GridPane.rowIndex="0" onAction="#handleEditAction" />
<Button text="Delete" GridPane.columnIndex="3"
GridPane.rowIndex="0" onAction="#handleDeleteAction" />
<ScrollPane GridPane.columnIndex="1" GridPane.rowIndex="1"
GridPane.columnSpan="3" />
<Label text="Speed" GridPane.columnIndex="1"
GridPane.rowIndex="2" />
<Slider GridPane.columnIndex="2" GridPane.rowIndex="2"
GridPane.columnSpan="2" />
<Label text="Gravity" GridPane.columnIndex="1"
GridPane.rowIndex="3" />
<TextField GridPane.columnIndex="2" GridPane.rowIndex="3"
GridPane.columnSpan="2" />
<HBox GridPane.columnIndex="1" GridPane.rowIndex="4">
<Button text="<<" onAction="#handleQuickRewindAction" />
<Button text="<" onAction="#handleRewindAction" />
</HBox>
<Text text="0.00" GridPane.columnIndex="2" GridPane.rowIndex="4" />
<HBox GridPane.columnIndex="3" GridPane.rowIndex="4">
<Button text=">" onAction="#handleFastForwardAction" />
<Button text=">>"
onAction="#handleQuickFastForwardAction" />
</HBox>
<Button text="Start" GridPane.columnIndex="2"
GridPane.rowIndex="5" onAction="#handlePauseAction" />
</GridPane>
</center>
<stylesheets>
<URL value="#style.css" />
</stylesheets>
</BorderPane>
The grey square in the image is the SubScene, with hard coded values so that it is visible. If values for the width and height are not hardcoded, it will simply not appear on the screen.
Firstly, there may be other ways as well, but this is my take on this issue :).
I would like to recommend you to rethink about the layout implementation. I think it would be better to keep the right side part in its own gridPane and you primarily deal with only 2 sections (center- subScene and right- form layout). That way you don't unneccesarily mess with layout by keeping too many eggs in one basket.
And regarding the SubScene, it does not fit in any of the standard layouts behavior (like no min/pref/max sizes). It pretty much has the same features as Shape, where you need to explicity set the width/height when required.
So for that, you need to do some explicit calculations to determine the width/height of the SubScene whenever the main scene size is changed.
Below is the quick demo for changes to your code: (If your window is a TRANSPARENT stage, then in the calculations, you may need to consider the window header size as well)
FXML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.SubScene?>
<?import javafx.scene.text.Text?>
<?import javafx.geometry.Insets?>
<BorderPane fx:id="mainLayout" fx:controller="Controller" xmlns:fx="http://javafx.com/fxml"
styleClass="root">
<top>
<MenuBar fx:id="menuBar">
<menus>
<Menu text="File">
<items>
<MenuItem text="New"
onAction="#handleNewSimulationAction"/>
<MenuItem text="Save"
onAction="#handleSaveSimulationAction"/>
<MenuItem text="Load"
onAction="#handleLoadSimulationAction"/>
</items>
</Menu>
</menus>
</MenuBar>
</top>
<right>
<GridPane fx:id="sideLayout" hgap="10" vgap="10">
<rowConstraints>
<RowConstraints vgrow="NEVER"/>
<RowConstraints vgrow="ALWAYS"/>
</rowConstraints>
<gridLinesVisible>true</gridLinesVisible>
<padding>
<Insets left="10"/>
</padding>
<Button text="Add" GridPane.columnIndex="0" GridPane.rowIndex="0" onAction="#handleAddAction"
prefWidth="-1" minWidth="-Infinity"/>
<Button text="Edit" GridPane.columnIndex="1" GridPane.rowIndex="0"
onAction="#handleEditAction" prefWidth="-1" minWidth="-Infinity"/>
<Button text="Delete" GridPane.columnIndex="2" GridPane.rowIndex="0"
onAction="#handleDeleteAction" prefWidth="-1" minWidth="-Infinity"/>
<ScrollPane GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnSpan="3"/>
<Label text="Speed" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<StackPane GridPane.columnIndex="1" GridPane.rowIndex="2" GridPane.columnSpan="2"/>
<Label text="Gravity" GridPane.columnIndex="0" GridPane.rowIndex="3"/>
<StackPane GridPane.columnIndex="1" GridPane.rowIndex="3" GridPane.columnSpan="2"/>
<HBox GridPane.columnIndex="0" GridPane.rowIndex="4">
<Button text="<<" onAction="#handleQuickRewindAction" prefWidth="-1"
minWidth="-Infinity"/>
<Button text="<" onAction="#handleRewindAction" prefWidth="-1" minWidth="-Infinity"/>
</HBox>
<Text text="0.00" GridPane.columnIndex="1" GridPane.rowIndex="4"/>
<HBox GridPane.columnIndex="2" GridPane.rowIndex="4">
<Button text=">" onAction="#handleFastForwardAction" prefWidth="-1"
minWidth="-Infinity"/>
<Button text=">>"
onAction="#handleQuickFastForwardAction" prefWidth="-1" minWidth="-Infinity"/>
</HBox>
<Button text="Start" GridPane.columnIndex="1" GridPane.rowIndex="5"
onAction="#handlePauseAction" prefWidth="-1" minWidth="-Infinity"/>
</GridPane>
</right>
<center>
<SubScene fx:id="subscene">
<root>
<StackPane style="-fx-background-color:yellow;-fx-border-width:1px;-fx-border-color:red;">
<Label text="Sub Scene" style="-fx-font-size:16px;"/>
</StackPane>
</root>
</SubScene>
</center>
</BorderPane>
Controller code:
#FXML
private MenuBar menuBar;
#FXML
private SubScene subscene;
#FXML
private GridPane sideLayout;
#FXML
public void initialize() {
subscene.sceneProperty().addListener((ob, ol, scene) -> {
scene.heightProperty().addListener((obs, old, height) -> updateHeight());
scene.widthProperty().addListener((obs, old, width) -> updateWidth());
});
sideLayout.heightProperty().addListener((obs, old, height) -> updateHeight());
sideLayout.widthProperty().addListener((obs, old, width) -> updateWidth());
}
private void updateHeight() {
double sceneHeight = subscene.getScene().getHeight();
subscene.setHeight(sceneHeight - menuBar.getHeight());
}
private void updateWidth() {
double sceneWidth = subscene.getScene().getWidth();
subscene.setWidth(sceneWidth - sideLayout.getWidth());
}

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.

Label does not get displayed in javafx

I have a borderpane in my application. In the bottom part i wanted to set a Label to display the current status of the program for example: "ready", "reading data" and so on.
When I start the application the bottom section does not get displayed. Only after I changed the height of the window manually with my mouse it suddenly pops up. Does anyone know why this happens? I suppose it has something to do with my listener.
main
package Sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View/MainWindow.fxml"));
primaryStage.setTitle("Tool-Name");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane fx:id="root" prefHeight="453.0" prefWidth="604.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.MainWindow_Controller">
<center>
<TabPane fx:id="tpTabs" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="402.0" prefWidth="604.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="tDatabase" text="XXXXXXX">
<content>
<AnchorPane fx:id="apDatabase" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="bSetDBPath" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="XXXXXXX..." />
<Button fx:id="bSetHeadFilePath" layoutX="14.0" layoutY="66.0" mnemonicParsing="false" text="XXXXXXX..." />
<TextField fx:id="tfDBPath" layoutX="138.0" layoutY="14.0" prefHeight="25.0" prefWidth="255.0" promptText="XXXXXXX" />
<TextField fx:id="tfHeadFilePath" layoutX="138.0" layoutY="66.0" prefHeight="25.0" prefWidth="255.0" promptText="XXXXXXX" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="XXXXXXX">
<content>
<Accordion>
<panes>
<TitledPane text="XXXXXXX">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="bGetNewMembers" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="XXXXXXX" />
<Button fx:id="bGetStatusChange" layoutX="14.0" layoutY="58.0" mnemonicParsing="false" text="XXXXXXX" />
<Button fx:id="bRegulation" layoutX="14.0" layoutY="101.0" mnemonicParsing="false" text="XXXXXXX" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane text="XXXXXXX">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="bAdministrationData" layoutX="14.0" layoutY="57.0" mnemonicParsing="false" text="XXXXXXX" />
<Button fx:id="bCompareStatus" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="XXXXXXX" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane text="XXXXXXX">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button fx:id="bOrganisation" layoutX="14.0" layoutY="103.0" mnemonicParsing="false" text="XXXXXXX" />
<Button fx:id="bDeferredCompensation" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" text="XXXXXXX" />
<Button fx:id="bCompanyMatching" layoutX="14.0" layoutY="60.0" mnemonicParsing="false" text="XXXXXXX" />
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
</Accordion>
</content>
</Tab>
<Tab fx:id="tTestCases" text="XXXXXXX">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tvTestCases" prefHeight="310.0" prefWidth="600.0">
<columns>
<TableColumn fx:id="tcTestCaseId" prefWidth="132.0" text="XXXXXXX">
<cellValueFactory><PropertyValueFactory property="testCaseId" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="tcPaymentId" prefWidth="150.0" text="XXXXXXX">
<cellValueFactory><PropertyValueFactory property="paymentId" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</Tab>
<Tab fx:id="tReports" text="XXXXXXX">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="tvReports" prefHeight="310.0" prefWidth="604.0">
<columns>
<TableColumn fx:id="tcReportId" editable="false" prefWidth="75.0" text="XXXXXXX" />
<TableColumn fx:id="tcReportType" editable="false" prefWidth="75.0" text="XXXXXXX" />
<TableColumn fx:id="tcCreationDate" editable="false" prefWidth="75.0" text="XXXXXXX" />
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</center>
<top>
<MenuBar fx:id="mbMenuBar" prefHeight="30.0" prefWidth="604.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="XXXXXXX">
<items>
<MenuItem mnemonicParsing="false" text="XXXXXXX" />
<MenuItem mnemonicParsing="false" text="XXXXXXX" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="XXXXXXX" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="XXXXXXX">
<items>
<Menu mnemonicParsing="false" text="XXXXXXX">
<items>
<MenuItem fx:id="miNewVoucher" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem fx:id="miNewAssociation" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem fx:id="miNewPensioner" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem fx:id="miNewRegulation" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem fx:id="miNewAdministrationCosts" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem fx:id="miNewPayment" mnemonicParsing="false" text="XXXXXXX" />
</items>
</Menu>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="miOpenDB" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem fx:id="miImportDatasets" mnemonicParsing="false" text="XXXXXXX" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="miSetDBPath" mnemonicParsing="false" text="XXXXXXX" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="XXXXXXX">
<items>
<MenuItem fx:id="miOpenManual" mnemonicParsing="false" text="XXXXXXX" />
<MenuItem mnemonicParsing="false" text="XXXXXXX" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem fx:id="miSetManualPath" mnemonicParsing="false" text="XXXXXXX" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<bottom>
<Label fx:id="lInfo" prefHeight="21.0" prefWidth="604.0" text="Bereit" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
Controller
package Controller;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
public class MainWindow_Controller {
#FXML
private BorderPane root;
#FXML
private TableView tvTestCases;
#FXML
private TableView<?> tvReports;
#FXML
MenuBar mbMenuBar;
#FXML
private TabPane tpTabs;
#FXML
private Label lInfo;
/**
* Initialize method to read filepaths and set listeners.
*/
public void initialize() {
tpTabs.setBackground(new Background(new BackgroundFill(Color.BISQUE, null, null)));
loadListener();
lInfo.setText("Bereit");
}
public void loadListener() {
root.widthProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
tpTabs.setPrefWidth(root.getWidth());
tvTestCases.setPrefWidth(root.getWidth());
tvReports.setPrefWidth(root.getWidth());
}
});
root.heightProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
tpTabs.setPrefHeight(root.getHeight() - mbMenuBar.getHeight()-lInfo.getHeight());
tvTestCases.setPrefHeight(root.getHeight() - mbMenuBar.getHeight()-lInfo.getHeight());
tvReports.setPrefHeight(root.getHeight()-mbMenuBar.getHeight()-lInfo.getHeight());
}
});
}
}
Thank you for any help
You shouldn't adjust the preferred sizes of the nodes during layout. What happens here is that the scene size is determined by calculating the preferred/min/max size before the root size is set. When the root size is set the listeners added in loadListener change the preferred sizes which no longer leaves enough room for all of the nodes. Since the Scene enforces it's size to be smaller than the actual min size of it's root now, the layouts accomodate to this in a some way, in this case by moving the Label out of view.
Do not add those listeners and leave the resizing to the BorderPane. For this to work you need to set the max sizes to sizes other than Region.USE_PREF_SIZE (= -1d/0d = Double.NEGATIVE_INFINITY).
The following opening tag for <TabPane> simply uses the default value (Region.USE_COMPUTED_SIZE). This way the TabPane can grow arbitrary large:
<TabPane fx:id="tpTabs" minHeight="-Infinity" minWidth="-Infinity" prefHeight="402.0" prefWidth="604.0" tabClosingPolicy="UNAVAILABLE">

How to set Nodes from FXML JavaFX?

I have created a Layout with FXML which consists of a BorderPane with a sideMenu and an innerBorderPane for other stuff...
But since I'm barely starting with FXML in JavaFX I just need to know how to do the next thing...
Custom.fxml (This is the main FXML layout)
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox fx:controller="Transcoro.Controllers.TabManager" alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="442.0" prefWidth="338.0" spacing="5.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox prefHeight="59.0" prefWidth="124.0">
<children>
<VBox prefHeight="59.0" prefWidth="90.0" style="-fx-background-color: blue;" />
<VBox alignment="CENTER_LEFT" prefHeight="59.0" prefWidth="165.0">
<children>
<Label text="Bienvenido, Rodolfo">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Label text="Administrador" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<HBox.margin>
<Insets />
</HBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</VBox>
</children>
<VBox.margin>
<Insets bottom="15.0" />
</VBox.margin>
</HBox>
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Unidades" textAlignment="CENTER" textFill="#2491ff" />
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Empleados" textAlignment="CENTER" textFill="#2491ff" />
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Clientes" textAlignment="CENTER" textFill="#2491ff" />
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Viajes" textAlignment="CENTER" textFill="#2491ff" />
</children>
<padding>
<Insets top="20.0" />
</padding>
</VBox>
sideMenu.fxml (This is the sideMenu FXML layout)
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<fx:root type="javafx.scene.layout.VBox" alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="442.0" prefWidth="338.0" spacing="5.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox prefHeight="59.0" prefWidth="124.0">
<children>
<VBox prefHeight="59.0" prefWidth="90.0" style="-fx-background-color: blue;" />
<VBox alignment="CENTER_LEFT" prefHeight="59.0" prefWidth="165.0">
<children>
<Label text="Bienvenido, Rodolfo">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<Label text="Administrador" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<HBox.margin>
<Insets />
</HBox.margin>
<padding>
<Insets left="10.0" />
</padding>
</VBox>
</children>
<VBox.margin>
<Insets bottom="15.0" />
</VBox.margin>
</HBox>
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Unidades" textAlignment="CENTER" textFill="#2491ff" />
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Empleados" textAlignment="CENTER" textFill="#2491ff" />
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Clientes" textAlignment="CENTER" textFill="#2491ff" />
<Button alignment="CENTER" contentDisplay="CENTER" maxWidth="1.7976931348623157E308" mnemonicParsing="false" nodeOrientation="LEFT_TO_RIGHT" text="Viajes" textAlignment="CENTER" textFill="#2491ff" />
</children>
<padding>
<Insets top="20.0" />
</padding>
</fx:root>
TabManager.java (This is the sideMenu JavaFX controller)
package Transcoro.Controllers;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
import java.io.IOException;
public class TabManager extends VBox {
public TabManager(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/resources/view/sideMenu.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
}
MainLayout.java (This is the mainLayout JavaFX Controller)
package Transcoro.Core;
import Transcoro.Controllers.TabManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.*;
import java.io.IOException;
public class MainLayout extends BorderPane {
#FXML
private VBox sideMenu;
#FXML
private BorderPane innerContent;
#FXML
private HBox upperMenu;
#FXML
private ScrollPane contentScroll;
#FXML
private VBox otherPane;
public MainLayout(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/resources/view/Custom.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
TabManager _tabs = new TabManager();
this.setSideMenu(_tabs);
}
What I want to do is the following:
I have two layouts, the base which is the main Layout and sideMenu layout in another FXML
When I instantiate the mainLayout in my root that works fine, then when I want to insert the TabManager (VBox) into sideMenu (VBox from FXML) it doesn't work... I'm doing this:
TabManager _tabs = new TabManager();
this.setSideMenu(_tabs);
The only way I have made it work is by adding:
this.setLeft(this.sideMenu);
OR
this.setLeft(_tabs);
Which I think it shouldn't work that why, because I'm already stating that sideMenu goes on the Left BorderPane side, any idea on how should I approach this?
Firstly, remove fx:controller="Transcoro.Controllers.TabManager" from Custom.fxml. The controller is MainLayout, which you will set via FXMLLoader.
Secondly, don't call fxmlLoader.setRoot(this); in MainLayout. You should instead retrieve the root node out from Custom.fxml, then add it to MainLayout, which itself is a BorderPane.
Parent root = fxmlLoader.getRoot();
this.setCenter(root);
This will put everything from Custom.fxml as the center child of MainLayout.
Thirdly, setLeft() is working because you are setting the TabManager instance (which is a VBox) as the left child of MainLayout. setSideMenu() (which is assumed as the setter of #FXML private VBox sideMenu does not work because sideMenu is just a reference injected into the controller. When you change this reference, it does not change the original layout.

having trouble with javafx program

I am in the process of creating a javafx desktop application. The problem I'm encountering is if I add a controller to my fxml files the controls dissapear somehow. The program uses a fade transition to switch between the fxml files
AnchorPane reorderLevels,purchaseorder,onlineSales,generalLedger
,cashBook,payments,departmentalTransfers
,purchaseInvoice
,productMantainance,
branchTransfers,stockMovement,invoiceRegistration,stockTake;
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Override
public void initialize(URL url, ResourceBundle rb) {
//load all the fxml screens when the buttons are clicked
try {
//screens for the application
reorderLevels = FXMLLoader.load(getClass().getResource("ReorderLevels.fxml"));
purchaseorder = FXMLLoader.load(getClass().getResource("PurchaseOrder.fxml"));
onlineSales = FXMLLoader.load(getClass().getResource("OnlineSales.fxml"));
generalLedger = FXMLLoader.load(getClass().getResource("GeneralLedger.fxml"));
cashBook = FXMLLoader.load(getClass().getResource("CashBook.fxml"));
departmentalTransfers = FXMLLoader.load(getClass().getResource("DepartmentalTransfers.fxml"));
purchaseInvoice = FXMLLoader.load(getClass().getResource("PurchaseInvoice.fxml"));
productMantainance = FXMLLoader.load(getClass().getResource("ProductMantainance.fxml"));
branchTransfers = FXMLLoader.load(getClass().getResource("BranchTransfers.fxml"));
stockMovement = FXMLLoader.load(getClass().getResource("StockMovement.fxml"));
payments = FXMLLoader.load(getClass().getResource("Payments.fxml"));
invoiceRegistration = FXMLLoader.load(getClass().getResource("InvoiceRegistration.fxml"));
stockTake = FXMLLoader.load(getClass().getResource("StockTake.fxml"));
//set each and every individual node when the buttons are clicked
setNode(reorderLevels);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void setNode(Node node) {
holderPane.getChildren().clear();
holderPane.getChildren().add((Node) node);
//the transition between the screens
FadeTransition ft = new FadeTransition(Duration.millis(1));
ft.setNode(node);
ft.setFromValue(0.1);
ft.setToValue(1);
ft.setCycleCount(1);
ft.setAutoReverse(false);
ft.play();
}
/* screens */
#FXML
private void switchReorderLevels(ActionEvent event) {
setNode(reorderLevels);
}
#FXML
private void switchOnlineSales(ActionEvent event) {
setNode(onlineSales);
}
#FXML
private void switchGeneralLedger(ActionEvent event) {
setNode(generalLedger);
}
#FXML
private void switchPurchaseOrder(ActionEvent event) {
setNode(purchaseorder);
}
#FXML
private void switchCashBook(ActionEvent event) {
setNode(cashBook);
}
#FXML
private void switchAlert(ActionEvent event) {
setNode(purchaseorder);
}
#FXML
private void switchPayments(ActionEvent event) {
setNode(payments);
}
#FXML
private void switchDepartmentalTransfers(ActionEvent event) {
setNode(departmentalTransfers);
}
#FXML
private void switchPurchaseInvoice(ActionEvent event) {
setNode(purchaseInvoice);
}
#FXML
private void switchProductMantainance(ActionEvent event) {
setNode(productMantainance);
}
#FXML
private void switchBranchTransfers(ActionEvent event) {
setNode(branchTransfers);
}
//this is a screen that will displayone of the layouts that will show re order levels
<AnchorPane id="AnchorPane" prefHeight="713.0" prefWidth="1219.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="24.0" layoutY="17.0" prefHeight="30.0" prefWidth="285.0" style="-fx-font-size: 20; -fx-font-weight: bold; -fx-text-fill: #7387A8;" text="ReOrder Levels" />
<Separator layoutX="25.0" layoutY="54.0" prefHeight="5.0" prefWidth="1191.0" />
<Pane fx:id="tbl_reorderLevels" layoutX="25.0" layoutY="108.0" prefHeight="413.0" prefWidth="1174.0" style="-fx-background-color: #EFEFEF;">
<children>
<JFXComboBox fx:id="cmd_reOrder_productCode" layoutX="48.0" layoutY="33.0" prefHeight="25.0" prefWidth="267.0" promptText="Product Code" />
<JFXTextField fx:id="txt_reOrder_reorderLevel" labelFloat="true" layoutX="48.0" layoutY="91.0" prefHeight="25.0" prefWidth="267.0" promptText="Reorder Level" />
//this is the root layout that has buttons that will navigate between the screens using a fade transition
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<!-- root layout -->
<AnchorPane id="AnchorPane" nodeOrientation="LEFT_TO_RIGHT" prefHeight="623.0" prefWidth="1271.0" style="-fx-background-color: #fff;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller = "dashboard.FXMLDocumentController">
<children>
<Pane layoutX="-2.0" prefHeight="81.0" prefWidth="1272.0" style="-fx-background-color: #0b8dee;" styleClass="head-background" stylesheets="#style.css" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label fx:id="labelEstock" layoutX="204.0" layoutY="-3.0" prefHeight="88.0" prefWidth="536.0" style="-fx-shape: round;" stylesheets="#custom.css" text="Oasys Estock" textFill="WHITE">
<font>
<Font name="Brush Script MT Italic" size="36.0" />
</font>
</Label>
<!-- <ImageView fx:id="imageEstock" fitHeight="110.0" fitWidth="92.0" layoutX="102.0" layoutY="8.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image
url="#../../../../AndroidStudioProjects/OasysEstock/app/src/main/res/drawable/fortified_icon.png" />
</image>
</ImageView> -->
</children>
</Pane>
<VBox layoutY="85.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="518.0" prefWidth="122.0" style="-fx-background-color: #0b8dee; -fx-background-radius: 15;">
<children>
<JFXButton id="btnReorderLevels" fx:id="btnReorderLevels" buttonType="RAISED" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onAction="#switchReorderLevels" prefHeight="47.0" prefWidth="107.0" style="-fx-background-radius: 100;" text="ReOrder Levels" textFill="#f5f0f0">
<VBox.margin>
<Insets />
</VBox.margin>
<font>
<Font name="Calibri" size="12.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnInvoiceRegistration" onAction="#switchInvoiceRegistration" prefHeight="25.0" prefWidth="121.0" text="Invoice Registration" textFill="#f8f4f4" />
<JFXButton fx:id="btnPurchaseOrder" buttonType="RAISED" onAction="#switchPurchaseOrder" prefHeight="34.0" prefWidth="107.0" text="Purchase Order" textFill="#f5f0f0">
<font>
<Font name="Cambria" size="12.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnOnlineSales" onAction="#switchOnlineSales" prefHeight="39.0" prefWidth="107.0" text="Online Sales" textFill="#fff9f9">
<font>
<Font name="Calibri" size="12.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnGeneralLedger" onAction="#switchGeneralLedger" prefHeight="40.0" prefWidth="108.0" text="General Ledger" textFill="#f2eeee" />
<JFXButton fx:id="btnCashBook" onAction="#switchCashBook" prefHeight="35.0" prefWidth="104.0" text="Cash Book" textFill="#fffefe" />
<JFXButton fx:id="btnPayments" buttonType="RAISED" onAction="#switchPayments" prefHeight="25.0" prefWidth="114.0" text="Payments" textFill="#fcf6f6" />
<JFXButton fx:id="btnDepartmentalTransfers" buttonType="RAISED" layoutX="10.0" layoutY="205.0" onAction="#switchDepartmentalTransfers" prefHeight="25.0" prefWidth="114.0" text="Dept Transfers" textFill="#fcf6f6" />
<JFXButton fx:id="btnProductMantainance" buttonType="RAISED" layoutX="10.0" layoutY="230.0" onAction="#switchProductMantainance" prefHeight="56.0" prefWidth="121.0" text="Product Mantainance" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnPurchaseInvoice" buttonType="RAISED" layoutX="10.0" layoutY="255.0" onAction="#switchPurchaseInvoice" prefHeight="56.0" prefWidth="121.0" text="Purchase Invoice" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnBranchTransfers" buttonType="RAISED" layoutX="10.0" layoutY="311.0" onAction="#switchBranchTransfers" prefHeight="56.0" prefWidth="121.0" text="Branch Transfers" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnStockMovement" buttonType="RAISED" layoutX="10.0" layoutY="367.0" onAction="#switchStockMovement" prefHeight="56.0" prefWidth="121.0" text="Stock Movement" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnStockTake" buttonType="RAISED" layoutX="10.0" layoutY="448.0" onAction="#switchStockTake" prefHeight="56.0" prefWidth="121.0" text="Stock Take" textFill="#fcf6f6">
<font>
<Font size="11.0" />
</font>
</JFXButton>
</children>
<effect>
<InnerShadow blurType="TWO_PASS_BOX" choke="0.58" color="#212122" height="26.69" radius="10.620000000000001" width="17.79" />
</effect>
</VBox>
<AnchorPane fx:id="holderPane" layoutX="125.0" layoutY="96.0" prefHeight="507.0" prefWidth="1134.0"/>
</children>
</AnchorPane>

Resources