fxml file not behaving as expected in OS X - macos

I am using an fxml file to to create my java fx UI.
the file is:-
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<AnchorPane fx:id="ap" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="480.0" prefWidth="840.0" xmlns="http://javafx.com/javafx/8.0.65"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.Buildsapp.Main.BuildsController">
<children>
<VBox prefHeight="200.0" prefWidth="100.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="CENTER_LEFT" spacing="20.0" VBox.vgrow="NEVER">
<padding>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</padding>
<children>
<GridPane hgap="10.0" HBox.hgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0"
vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label prefHeight="21.0" prefWidth="88.0" text="Platform" />
<ComboBox fx:id="versionCombo" prefHeight="31.0"
prefWidth="108.0" GridPane.rowIndex="1">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Win" />
<String fx:value="Mac" />
</FXCollections>
</items>
<value>
<String fx:value="Mac" />
</value>
</ComboBox>
<Label prefHeight="21.0" prefWidth="80.0" text="Product"
GridPane.columnIndex="1" />
<ComboBox fx:id="verCombo" prefHeight="31.0" prefWidth="127.0"
promptText="build" GridPane.columnIndex="1" GridPane.rowIndex="1">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="build" />
<String fx:value="build2" />
</FXCollections>
</items>
</ComboBox>
<Label text="Version" GridPane.columnIndex="2" />
<ComboBox fx:id="versionNo" prefHeight="31.0"
prefWidth="109.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label prefHeight="21.0" prefWidth="60.0" text="Server"
GridPane.columnIndex="3" />
<ComboBox fx:id="locCombo" prefHeight="31.0" prefWidth="103.0"
GridPane.columnIndex="3" GridPane.rowIndex="1">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="SJ" />
<String fx:value="MN" />
</FXCollections>
</items>
<value>
<String fx:value="SJ" />
</value>
</ComboBox>
<Button fx:id="downloadButton" minWidth="80.0"
mnemonicParsing="false" text="Download" GridPane.columnIndex="4"
GridPane.rowIndex="1" />
<Button fx:id="installButton" minWidth="80.0"
mnemonicParsing="false" text="Install" GridPane.columnIndex="5"
GridPane.rowIndex="1" />
<Button fx:id="locButton" mnemonicParsing="false"
prefHeight="31.0" prefWidth="120.0" text="Open Folder"
GridPane.columnIndex="6" GridPane.rowIndex="1" />
</children>
</GridPane>
</children>
</HBox>
<TableView fx:id="tableView" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="builds" prefWidth="482.0" text="Builds" />
<TableColumn fx:id="date" minWidth="0.0" prefWidth="500.0"
text="Date" />
</columns>
</TableView>
</children>
</VBox>
</children>
</AnchorPane>
It works fine in windows while in mac when I launch the app the app window does not appear as expected but it appears as a small window on the top left corner.
The similar issue is appearing with the progress dialog which I am using it works fine in Windows but in mac(OS X) when the app is expanded like when app window size is maximum the progress dialog is also covering the whole display screen while I have given fixed size to scene window.
This is my progress bar code:-
public ProgressBar startProgressBar() {
primaryStage = new Stage();
ProgressBar pb = new ProgressBar(0);
//ProgressIndicator pi = new ProgressIndicator(0);
//pi.progressProperty().bind(pb.progressProperty());
HBox hb = new HBox();
hb.setSpacing(5);
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(pb);
Scene scene = new Scene(hb, 300, 100);
primaryStage.setScene(scene);
primaryStage.setTitle("Downloading Build...");
primaryStage.show();
return pb;
}

I am able find the solution for the first issue that can be solved my fixing the minHight and minWidth of the anchor panel to some value in fxml file instead of infinity but I am still not able to find the solution for the second issue.

Related

How to load an fxml file from a controller that is linked to a different fxml file?

I am building a JavaFX which has two windows: a signin window and a register window. The fxml file of the former has a "username" label and text field, a "password" label and text field, a "signin" button, a "close" button and a hyperlink that reads "or create a new user". I want that when users click on the hyperlink, a registration window pop-up with labels and text fields asking for the user to enter a user name, password, first name, etc.
I have a Login.fxml file and a LoginController.java file for the sign in window. And for the registration window I have a CreateUser.fxml file and a CreateUSerController.java file.
My problem is how to load the CreateUser.fxml file (and make the registration window pop-up) from the LoginController.java file. I handle the clicking on the "or create a new user" hyperlink event in LoginController.java, and basically the result I expect of the handling is to make the registration window to pop-up. I get a runtime error.
When I click on the "or create a new user" hyperlink in the login window I get the following error: The Eclipse debugger says "Thread[JavaFX Application Thread](Suspended(uncaught exception RuntimeException))". Also, when I check the value of the variables during execution there is a "runWithoutRenderLock() is throwing", and its value is RuntimeException. Also says that its cause is a "InvocationTargetException", and that the cause of this exception is a "null". Also, a "QuantumToolkit.class" tab appears in eclipse with the message "Source not found" "The JAR file ... has no source attachment". It also has a clickable button that reads "Attach source"
To load "CreateUser.fxml" from "LoginController.java" I used a handler which tries to load the fxml file with this line of code: "root = FXMLLoader.<Parent>load(CreateUserController.class.getResource("CreateUser.fxml"));". Here is where the exception occurs
Please find below my Main class which is where I load the "Login.fxml" file, the "Login.fxml", "LoginController.java", "CreateUser.fxml" and CreateUSerController.java files. Also, an image of my file structure.
File structure
src
/application
-Main.java
/controller
-LoginController.java
-CreateUserController.java
/model
/view
-Login.fxml
-CreateUser.fxml
-module-info.java
Main.java
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage signinStage) throws IOException {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
Scene scene = new Scene(root,400,400);
signinStage.setTitle("Welcome to MyHEalth");
signinStage.setScene(scene);
signinStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
launch(args);
}
}
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<BorderPane xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.LoginController">
<center>
<VBox alignment="CENTER" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" BorderPane.alignment="CENTER">
<children>
<Label maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="21.0" prefWidth="300.0" text="Username">
<VBox.margin>
<Insets bottom="10.0" />
</VBox.margin>
</Label>
<TextField fx:id="userNameField" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="300.0">
<VBox.margin>
<Insets bottom="20.0" />
</VBox.margin>
</TextField>
<Label prefHeight="17.0" prefWidth="300.0" text="Password">
<VBox.margin>
<Insets bottom="10.0" />
</VBox.margin>
</Label>
<TextField fx:id="passwordField" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="300.0" />
<BorderPane prefHeight="100.0" prefWidth="200.0">
<center>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="30.0" prefWidth="170.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="signinButton" maxWidth="-Infinity" mnemonicParsing="false" onAction="#signin" prefWidth="70.0" text="Sign in" />
<Button fx:id="closeButton" alignment="CENTER" maxWidth="-Infinity" mnemonicParsing="false" onAction="#close" prefWidth="60.0" text="Close" textAlignment="RIGHT">
<HBox.margin>
<Insets left="40.0" />
</HBox.margin>
</Button>
</children>
</HBox>
</center>
<bottom>
<Hyperlink fx:id="newUserLink" onAction="#openNewUserWindow" text="or create new user" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
</children>
</VBox>
</center>
</BorderPane>
LoginController.java
package controller;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import model.User;
public class LoginController {
#FXML
private Button closeButton;
#FXML
private Hyperlink newUserLink;
#FXML
private TextField passwordField;
#FXML
private Button signinButton;
#FXML
private TextField userNameField;
private Stage stage;
private Parent root;
#FXML
void signin(ActionEvent event) {
passwordField.getText();
userNameField.getText();
User user = new User(passwordField.getText(), "first name", "last name", userNameField.getText(), "imagepath");
//still have to get the rest of the information from the database
System.out.print(user.getProfile().getFirstName());//debugging message
}
#FXML
void close(ActionEvent event) {
System.out.print("Thanks for visiting MyHealth. Bye");
System.exit(0);
}
#FXML
void openNewUserWindow(ActionEvent event) {
try {
System.out.print("1st line Inside 'try' block of 'openNewUserWindow' in LoginController");
//System.out.println("\nJust after FXMLLoader Inside 'try' block of 'openNewUserWindow' in LoginController");
root = FXMLLoader.<Parent>load(CreateUserController.class.getResource("CreateUser.fxml"));
//System.out.print("last line Inside 'try' block of 'openNewUserWindow' in LoginController");
}
catch(IOException e){
System.out.print("Problem getting 'CreateUSer.fxml'");
}
Stage createUserStage = new Stage();
Scene createUserScene = new Scene(root, 400, 400);
createUserStage.setScene(createUserScene);
createUserStage.setTitle("Create a new user");
createUserStage.show();
}
}
CreateUser.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.CreateUserController">
<children>
<VBox layoutY="30.0" prefHeight="440.0" prefWidth="400.0">
<children>
<ImageView fitHeight="82.0" fitWidth="100.0" pickOnBounds="true" preserveRatio="true">
<VBox.margin>
<Insets left="150.0" top="10.0" />
</VBox.margin>
</ImageView>
<Label fx:id="clickImage" text="Click to select profile picture" textFill="#00000093">
<VBox.margin>
<Insets bottom="20.0" left="125.0" />
</VBox.margin>
</Label>
<Label text="Username">
<VBox.margin>
<Insets bottom="5.0" left="50.0" />
</VBox.margin>
</Label>
<TextField fx:id="userName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="280.0">
<VBox.margin>
<Insets bottom="10.0" left="50.0" />
</VBox.margin>
</TextField>
<Label text="First name">
<VBox.margin>
<Insets bottom="5.0" left="50.0" />
</VBox.margin>
</Label>
<TextField fx:id="firstName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="280.0">
<VBox.margin>
<Insets bottom="10.0" left="50.0" />
</VBox.margin>
</TextField>
<Label text="Last name">
<VBox.margin>
<Insets bottom="5.0" left="50.0" />
</VBox.margin>
</Label>
<TextField fx:id="lastName" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="280.0">
<VBox.margin>
<Insets bottom="10.0" left="50.0" />
</VBox.margin>
</TextField>
<Label text="Password">
<VBox.margin>
<Insets bottom="5.0" left="50.0" />
</VBox.margin>
</Label>
<TextField fx:id="password" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="280.0">
<VBox.margin>
<Insets bottom="10.0" left="50.0" />
</VBox.margin>
</TextField>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="createButton" mnemonicParsing="false" onAction="#create" text="Create user">
<HBox.margin>
<Insets left="100.0" top="15.0" />
</HBox.margin>
</Button>
<Button fx:id="closeButton" mnemonicParsing="false" onAction="#close" text="Close">
<HBox.margin>
<Insets left="30.0" top="15.0" />
</HBox.margin>
</Button>
</children>
</HBox>
<Label fx:id="createdLabel" text="Created user" textFill="#2b784f">
<VBox.margin>
<Insets left="125.0" top="20.0" />
</VBox.margin>
</Label>
</children>
</VBox>
</children>
</VBox>
</children>
</AnchorPane>
CreateUSerController.java
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class CreateUserController {
#FXML
private Label clickImage;
#FXML
private Button closeButton;
#FXML
private Button createButton;
#FXML
private Label createdLabel;
#FXML
private TextField firstName;
#FXML
private TextField lastName;
#FXML
private TextField password;
#FXML
private TextField userName;
#FXML
void close(ActionEvent event) {
}
#FXML
void create(ActionEvent event) {
}
}
First of all, I personally prefer to instantiate a new FXMLLoader instance to load a fxml file. Then you can also specify a location and some other kind of stuff like a controller factory.
You can lookup the setter methods here: https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/FXMLLoader.html
But in this case your only mistake is that you load the fxml from the wrong class location.
When you use "CreateUserController.class.getResource("CreateUser.fxml")", it will try to find the fxml file in the same directory like the class. You have to use another path like you did before with the login fxml file. ("/view/CreateUser.fxml")
I hope that helps.

ControlsFX Validation GUI Shift

in my project I am building a JavaFX GUI that looks like this:
Everything works as I imagine.
For the validation of the text fields I would like to use ControlsFX Validation.
When I add these three lines of code and press the button so that the FXML and controller are loaded, the buttons move:
ValidationSupport val = new ValidationSupport();
val.registerValidator(this.firstNameTextfield, Validator.createEmptyValidator("Muss gefüllt sein"));
val.registerValidator(this.lastNameTextfield, Validator.createEmptyValidator("Muss gefüllt sein"));
my GUI behaves completely different. Buttons wiggle back and forth when pressed, like this:
When you leave the button with the mouse it becomes normal again.
Once I delete these three lines of code, the GUI works normally.
Once this error occurs the first time, it does not repeat in the running program.
It always happens only at the first button press.
MainApplication:
public class MainApplication extends Application {
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getClassLoader().getResource("de/bachelorarbeit/gui" +
"/mainwindow/main-application-view.fxml"));
Parent root = fxmlLoader.load();
Scene scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.show();
}
}
MainController:
/**
* controller for main-application-view.fxml
*/
public class MainController implements Initializable {
/**
* gui element
*/
#FXML
private BorderPane borderPane;
/** the instances are saved to save inputs when switching scenes **/
/** instanc of the home **/
private Parent homeRoot;
/** instanc of the home **/
private Parent customerRoot;
/** instanc of the article **/
private Parent articleRoot;
/** instanc of the order **/
private Parent orderRoot;
private double x, y;
#Override
public void initialize(URL location, ResourceBundle resources) {
//set home in the center
this.callHomeUI();
}
/**
* method to load a ui component
* #param ui path of fxml to be loaded
* #return instanc of Parent
*/
private Parent loadUI(String ui) {
Parent root;
try {
FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getClassLoader().getResource(ui));
root = fxmlLoader.load();
} catch (IOException e) {
//TODO Logger
throw new RuntimeException(e);
}
return root;
}
/**
* method to set the home in the center
*/
#FXML
public void callHomeUI(){
if (this.homeRoot == null) {
this.homeRoot = this.loadUI("de/bachelorarbeit/gui/home/home-view.fxml");
}
this.borderPane.setCenter(this.homeRoot);
}
/**
* method to set the home in the customer
*/
#FXML
public void callCustomerUI() {
if (this.customerRoot == null) {
this.customerRoot = this.loadUI("de/bachelorarbeit/gui/customer/customer-view.fxml");
}
this.borderPane.setCenter(this.customerRoot);
}
/**
* method to close the window
*/
#FXML
public void close() {
DataBaseManager.shutDown();
Stage stage = (Stage) this.borderPane.getScene().getWindow();
stage.close();
}
}
CustomerController with the three lines of code:
/**
* controller for customer-view.fxml
*/
public class CustomerController extends CustomerAbstractController implements Initializable {
/** gui elements **/
#FXML
private TextField firstNameTextfield;
#FXML
private TextField lastNameTextfield;
#FXML
private TextField emailTextfield;
#FXML
private DatePicker dateOfBirthPicker;
#FXML
private TextField cityTextfield;
#FXML
private TextField postalCodeTextfield;
#FXML
private TextField streetTextfield;
#FXML
private TextField houseNumberTextfield;
#FXML
private TableView<Customer> customerTableView;
#FXML
private TextField firstNameSearchTextfield;
#FXML
private TextField lastNameSearchTextfield;
#FXML
private TextField emailSearchTextfield;
#Override
public void initialize(URL location, ResourceBundle resources) {
//set listener and build customer table for search
ValidationSupport val = new ValidationSupport();
val.registerValidator(this.firstNameTextfield, Validator.createEmptyValidator("Muss gefüllt sein"));
val.registerValidator(this.lastNameTextfield, Validator.createEmptyValidator("Muss gefüllt sein"));
val.registerValidator(this.emailTextfield, Validator.createEmptyValidator("Muss gefüllt sein"));
}
}
MainApplication FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="borderPane" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bachelorarbeit.gui.mainwindow.MainController">
<left>
<VBox prefHeight="1080.0" prefWidth="149.0" style="-fx-background-color: #303030;" BorderPane.alignment="CENTER">
<children>
<VBox prefHeight="633.0" prefWidth="149.0">
<children>
<Button mnemonicParsing="false" onAction="#callHomeUI" prefHeight="40.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-text-fill: #f0f0f0; -fx-font-size: 15;" styleClass="sideButtons" stylesheets="#../../css/styles.css" text="Home">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Button>
<Button mnemonicParsing="false" onAction="#callCustomerUI" prefHeight="40.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-text-fill: #f0f0f0; -fx-font-size: 15;" styleClass="sideButtons" stylesheets="#../../css/styles.css" text="Kunde">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Button>
<Button mnemonicParsing="false" onAction="#callArticleUI" prefHeight="40.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-text-fill: #f0f0f0; -fx-font-size: 15;" styleClass="sideButtons" stylesheets="#../../css/styles.css" text="Artikel">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Button>
<Button mnemonicParsing="false" onAction="#callOrderUI" prefHeight="40.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-text-fill: #f0f0f0; -fx-font-size: 15;" styleClass="sideButtons" stylesheets="#../../css/styles.css" text="Bestellung">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Button>
</children>
</VBox>
<Button mnemonicParsing="false" onAction="#close" prefHeight="40.0" prefWidth="200.0" style="-fx-background-color: transparent; -fx-text-fill: #f0f0f0; -fx-font-size: 15;" styleClass="sideButtons" stylesheets="#../../css/styles.css" text="Schließen">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</Button>
</children>
</VBox>
</left>
</BorderPane>
Customer FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<TabPane prefHeight="720.0" prefWidth="1080.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.bachelorarbeit.gui.customer.CustomerController">
<tabs>
<Tab text="Kunde anlegen">
<content>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<GridPane prefHeight="691.0" prefWidth="771.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="313.857177734375" minWidth="10.0" prefWidth="122.5714111328125" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="556.2857055664062" minWidth="10.0" prefWidth="224.14288330078125" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="556.2857055664062" minWidth="10.0" prefWidth="142.85711669921875" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="556.2857055664062" minWidth="10.0" prefWidth="351.8571472167969" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="10.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="286.4285888671875" minHeight="10.0" prefHeight="116.71429443359375" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Bitte füllen Sie alle Angaben aus, um einen neuen Kunden anlegen zu können." GridPane.columnSpan="4" />
<Label text="Vorname:" GridPane.rowIndex="1" />
<Label text="Nachname:" GridPane.rowIndex="2" />
<Label text="E-Mail:" GridPane.rowIndex="3" />
<TextField fx:id="firstNameTextfield" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="lastNameTextfield" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField fx:id="emailTextfield" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label text="Geburtstag:" GridPane.rowIndex="4" />
<DatePicker fx:id="dateOfBirthPicker" prefHeight="25.0" prefWidth="250.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label text="Stadt:" GridPane.rowIndex="5">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<TextField fx:id="cityTextfield" GridPane.columnIndex="1" GridPane.rowIndex="5">
<GridPane.margin>
<Insets />
</GridPane.margin>
</TextField>
<Label text="Postleitzahl:" GridPane.columnIndex="2" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
<TextField fx:id="postalCodeTextfield" maxWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="5">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin>
</TextField>
<Label text="Strasse:" GridPane.rowIndex="6" />
<TextField fx:id="streetTextfield" GridPane.columnIndex="1" GridPane.rowIndex="6" />
<Label text="Hausnummer:" GridPane.columnIndex="2" GridPane.rowIndex="6">
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
<TextField fx:id="houseNumberTextfield" maxWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="6" />
<Button mnemonicParsing="false" text="Anlegen" GridPane.rowIndex="8" />
</children>
<padding>
<Insets left="20.0" />
</padding>
</GridPane>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Kunde bearbeiten / löschen">
<content>
<GridPane fx:id="bearbeitenGridPane" prefHeight="692.0" prefWidth="1080.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="82.0" minWidth="82.0" prefWidth="82.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="85.0" minWidth="85.0" prefWidth="85.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="85.0" minWidth="85.0" prefWidth="85.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="200.0" minWidth="200.0" prefWidth="200.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="20.0" minHeight="20.0" prefHeight="20.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="637.5714416503906" minHeight="10.0" prefHeight="590.4285583496094" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="An dieser Stelle können Kunden bearbeitet sowie gelöscht werden." GridPane.columnSpan="4">
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
</Label>
<TextField fx:id="firstNameSearchTextfield" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="lastNameSearchTextfield" GridPane.columnIndex="3" GridPane.rowIndex="1" />
<TextField fx:id="emailSearchTextfield" GridPane.columnIndex="5" GridPane.rowIndex="1" />
<Label text="Vorname:" GridPane.halignment="RIGHT" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="20.0" />
</GridPane.margin>
<padding>
<Insets right="10.0" />
</padding>
</Label>
<Label text="Nachname:" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="1">
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</Label>
<Label text="E-Mail:" GridPane.columnIndex="4" GridPane.halignment="RIGHT" GridPane.rowIndex="1">
<padding>
<Insets left="10.0" right="10.0" />
</padding>
</Label>
<Button mnemonicParsing="false" stylesheets="#../../css/styles.css" text="Suchen" GridPane.columnIndex="6" GridPane.halignment="CENTER" GridPane.rowIndex="1">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Button>
<Separator prefWidth="200.0" GridPane.columnSpan="7" GridPane.rowIndex="2" />
<TableView fx:id="customerTableView" stylesheets="#../../css/styles.css" GridPane.columnSpan="7" GridPane.rowIndex="3">
<GridPane.margin>
<Insets bottom="20.0" left="20.0" right="20.0" />
</GridPane.margin>
</TableView>
</children>
</GridPane>
</content>
</Tab>
</tabs>
</TabPane>
Home FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="720.0" prefWidth="1080.0" style="-fx-background-color: white;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<GridPane prefHeight="720.0" prefWidth="1080.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="195.42855834960938" minWidth="10.0" prefWidth="130.85711669921875" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="299.5714416503906" minWidth="10.0" prefWidth="268.14288330078125" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="130.0" minHeight="130.0" prefHeight="130.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="10.0" minHeight="10.0" prefHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="10.0" minHeight="10.0" prefHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="10.0" minHeight="10.0" prefHeight="10.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Thema: " GridPane.columnSpan="4" GridPane.rowIndex="4">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<ImageView fitHeight="127.0" fitWidth="383.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.halignment="CENTER">
<image>
<Image url="#../images/Ostfalia_LS_RGB_klein.jpg" />
</image>
<GridPane.margin>
<Insets top="20.0" />
</GridPane.margin>
</ImageView>
<Label text="Bachelorarbeit zur Erlangung" GridPane.columnSpan="4" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="18.0" />
</font>
<GridPane.margin>
<Insets bottom="40.0" left="40.0" />
</GridPane.margin>
</Label>
<Label text="des akademischen Grades Bachelor of Science" GridPane.columnSpan="4" GridPane.valignment="BOTTOM">
<font>
<Font name="System Bold" size="18.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="40.0" />
</GridPane.margin>
</Label>
<Label text="Studiengang: " GridPane.columnSpan="4" GridPane.rowIndex="3">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<Label text="Hochschule: " GridPane.columnSpan="4" GridPane.rowIndex="2">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<Label text="Student:" GridPane.columnSpan="2" GridPane.rowIndex="6">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<Label text="XXX" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.halignment="LEFT" GridPane.rowIndex="7" GridPane.valignment="TOP">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets />
</GridPane.margin>
</Label>
<Label text="Erstgutachter:" GridPane.columnSpan="2" GridPane.rowIndex="9">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<Label text="Zweitgutachter:" GridPane.columnSpan="2" GridPane.rowIndex="10">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<Label text="Abgabe:" GridPane.columnSpan="2" GridPane.rowIndex="11">
<font>
<Font size="18.0" />
</font>
<GridPane.margin>
<Insets left="40.0" />
</GridPane.margin>
</Label>
<Separator prefWidth="200.0" GridPane.columnSpan="4" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="20.0" right="20.0" />
</GridPane.margin>
</Separator>
<Separator prefWidth="200.0" GridPane.columnSpan="4" GridPane.rowIndex="5">
<GridPane.margin>
<Insets left="20.0" right="200.0" />
</GridPane.margin>
</Separator>
<Separator prefWidth="200.0" GridPane.columnSpan="4" GridPane.rowIndex="8">
<GridPane.margin>
<Insets left="20.0" right="200.0" />
</GridPane.margin>
</Separator>
<Label text="Hochschule für angewandte WissenschaftenHochschule Braunschweig/Wolfenbüttel" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="2">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="Informatik Software Engineering" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="3">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="Modellbasierte Entwicklung eines Order-To-Cash-Prozesses" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="4">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="XXXX" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="6">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="XX" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="9">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="XX" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="10">
<font>
<Font size="18.0" />
</font>
</Label>
<Label text="XX.07.2022" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowIndex="11">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</GridPane>
</children>
</AnchorPane>
Does anyone have any idea why this happens?
Thanks in advance.
I'll post an answer, even though I have no answer.
I think your issue is a bug. I don't think it is a bug in your code.
It is likely a bug in ControlsFX, or it could be (much less likely) in the JavaFX CSS processor.
I was able to recreate your issue by taking your code and making modifications to allow it to run (e.g. adding imports, removing references to code that is not there, removing references to the missing CSS stylesheet, adding a module-info, adding an opens clause to allow controlsfx to access the scene, etc). To do this I used JavaFX 18 and JDK 18 on OS X (Intel). After that, it could run.
It did not reproduce the shifting button issue you mention.
However, it did produce warning messages in the console:
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Could not resolve '-fx-text-background-color' while resolving lookups for '-fx-text-fill' from rule '*.label' in stylesheet jar:file:///Users/x/.m2/repository/org/openjfx/javafx-controls/18.0.1/javafx-controls-18.0.1-mac.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Could not resolve '-fx-text-background-color' while resolving lookups for '-fx-text-fill' from rule '*.label' in stylesheet jar:file:///Users/x/.m2/repository/org/openjfx/javafx-controls/18.0.1/javafx-controls-18.0.1-mac.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Caught 'java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.scene.paint.Color (java.lang.String is in module java.base of loader 'bootstrap'; javafx.scene.paint.Color is in module javafx.graphics#18.0.1 of loader 'app')' while converting value for '-fx-background-color' from rule '*.text-input' in stylesheet jar:file:///Users/x/.m2/repository/org/openjfx/javafx-controls/18.0.1/javafx-controls-18.0.1-mac.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Caught 'java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.scene.paint.Color (java.lang.String is in module java.base of loader 'bootstrap'; javafx.scene.paint.Color is in module javafx.graphics#18.0.1 of loader 'app')' while converting value for '-fx-highlight-fill' from rule '*.text-input' in stylesheet jar:file:///Users/x/.m2/repository/org/openjfx/javafx-controls/18.0.1/javafx-controls-18.0.1-mac.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Could not resolve '-fx-text-inner-color' while resolving lookups for '-fx-text-fill' from rule '*.text-input' in stylesheet jar:file:///Users/x/.m2/repository/org/openjfx/javafx-controls/18.0.1/javafx-controls-18.0.1-mac.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
WARNING: Could not resolve '-fx-text-inner-color' while resolving lookups for '-fx-highlight-text-fill' from rule '*.text-input' in stylesheet jar:file:///Users/x/.m2/repository/org/openjfx/javafx-controls/18.0.1/javafx-controls-18.0.1-mac.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
Jun 07, 2022 5:35:23 PM javafx.scene.CssStyleHelper calculateValue
The above is just a snippet, hundreds of similar warnings were generated.
These indicate that the CSS system has been fundamentally broken because it is no longer resolving the mandatory looked-up colors it needs to render the scene correctly.
Nevertheless, it appeared to render OK to my screen. However, I wouldn't trust the application to continue working with so many serious warnings logged.
I removed all style values from your scene to see if they were causing some issue, but that didn't help.
I changed the validation code in the customer controller to:
ValidationSupport val = new ValidationSupport();
val.registerValidator(this.firstNameTextfield, Validator.createEmptyValidator("A"));
val.registerValidator(this.lastNameTextfield, Validator.createEmptyValidator("B"));
If you remove all but one of the validators (e.g. only register the validator "A" from the example I provided), then it works. It only fails when there are multiple validators (probably it needs to run a different code path to validate multiple fields).
I reverted the JavaFX version from 18 to 11 and it worked. So this issue is likely because of changes in later versions of JavaFX with which ControlsFX 11 is not compatible (I don't know what that would be). Because of that, I suggest that you file a bug report with ControlsFX (as previously suggested in the comments), you can link back to this question if you file an issue. If you file an issue, it will be more likely to be fixed if you provide a targeted minimal example when doing so (please read and understand the link again), as well as all version info, execution command text, build file, and console logs.
However, due to an unrelated, incompatibility of the earlier JavaFX version with later Mac OS X versions, when I switch to JavaFX 11, all text is garbled, so it is completely unusable, even though it no longer logs CSS processing errors due to adding the validation code.
Interestingly, the CSS warnings are only generated when the customer fxml is loaded into the center of the border pane that was loaded in your original main fxml. If you just set the customer fxml as the root of the scene, the error does not manifest (I don't know why this is). So you can probably work around this issue by a UI redesign (though it would be unfortunate to have to work around an issue like this in such a way).

javafx: second stage with fxml doesn't react

I'm trying to use a searchbox in another stage by using fxml for the layout.
It it showing absolutely correct,but it doesn't react to any events.It seems that the elements are not found.
What is wrong with my code?
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
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;
public class SearchBox{
//tabs
#FXML
private static TabPane frepl_tabPane=new TabPane();
#FXML
private static Tab frepl_tabReplace=new Tab();
#FXML
private static Tab frepl_tabSearch=new Tab();
//search
//search Controls
#FXML
private static TextField fr_searchInput=new Textfield();
#FXML
private static Button fr_find=new Button();
private static Stage stage = new Stage();
public static void display(String title) throws IOException {
Parent root = FXMLLoader.load(SearchBox.class.getResource("find_replace.fxml"));
Scene scene = new Scene(root);
stage.setTitle(title);
stage.setScene(scene);
stage.showAndWait();
frepl_tabPane.getSelectionModel().selectedItemProperty().addListener((obs,ov,nv)->{
//no reaction here
stage.setTitle(nv.getText());
System.out.println("pane was tabbed");
});
fr_find.setOnMouseClicked(e ->{
//no reaction here
System.out.println("search");
});
}
To open it i use (inside of the "initialize" function of another FXMLDocumentControler):
btn_search.setOnMouseClicked(e->{
try {
SearchBox.display("search");
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
});
Here is the stack trace(when everything is non static e.g. the class,methods and fields). it appears,when i close the window.
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at SearchBox.display(SearchBox.java:133)
at FXMLDocumentController.lambda$A_table_buttons$22(FXMLDocumentController.java:1309)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="508.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TabPane fx:id="frepl_tabPane" layoutX="-1.0" layoutY="2.0" prefHeight="400.0" prefWidth="508.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="frepl_tabSearch" text="Search">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="fr_Stableview" layoutY="87.0" prefHeight="287.0" prefWidth="508.0">
<columns>
<TableColumn fx:id="fr_Sline" editable="false" prefWidth="75.0" sortable="false" text="Line" />
<TableColumn fx:id="fr_SColumn" editable="false" prefWidth="61.0" sortable="false" text="Column" />
<TableColumn fx:id="fr_SValue" editable="false" prefWidth="371.0" sortable="false" text="Value" />
</columns>
</TableView>
<TextField fx:id="fr_searchInput" layoutX="66.0" layoutY="13.0" prefHeight="25.0" prefWidth="250.0" />
<Label layoutX="3.0" layoutY="17.0" text="Search for:" />
<Button fx:id="fr_find" layoutX="324.0" layoutY="13.0" mnemonicParsing="false" text="Find" />
<Button fx:id="fr_findAll" layoutX="372.0" layoutY="13.0" mnemonicParsing="false" text="Find All" />
<CheckBox fx:id="fr_matchCaseS" layoutX="324.0" layoutY="53.0" mnemonicParsing="false" text="Match case (Aa)" />
<Label fx:id="fr_foundS" layoutX="8.0" layoutY="70.0" text="found: 5" underline="true">
<font>
<Font name="System Bold Italic" size="12.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
</Tab>
<Tab fx:id="frepl_tabReplace" text="Replace">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TableView fx:id="fr_tableviewR" layoutY="107.0" prefHeight="267.0" prefWidth="508.0">
<columns>
<TableColumn fx:id="fr_lineR" prefWidth="75.0" text="Line" />
<TableColumn fx:id="fr_ColumnR" prefWidth="61.0" text="Column" />
<TableColumn fx:id="fr_ValueR" prefWidth="371.0" text="Value" />
</columns>
</TableView>
<CheckBox fx:id="fr_matchCaseR" layoutX="347.0" layoutY="59.0" mnemonicParsing="false" text="Match case (Aa)" />
<Label layoutX="3.0" layoutY="17.0" text="Search for:" />
<Button fx:id="fr_replace" layoutX="347.0" layoutY="11.0" mnemonicParsing="false" text="Replace" />
<TextField fx:id="fr_replaceInput" layoutX="82.0" layoutY="13.0" prefHeight="25.0" prefWidth="250.0" />
<Button fx:id="fr_replaceAll" layoutX="412.0" layoutY="11.0" mnemonicParsing="false" text="Replace All" />
<TextField fx:id="fr_replaceAllInput" layoutX="82.0" layoutY="55.0" prefHeight="25.0" prefWidth="250.0" />
<Label layoutX="3.0" layoutY="59.0" text="Replace with:" />
<Label fx:id="fr_replacedR" layoutX="3.0" layoutY="90.0" text="replaced: 5" underline="true">
<font>
<Font name="System Bold Italic" size="12.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</Pane>

Populating ComboBox with JAVA FX MVC

I need to populate JAVAFX ComboBox with value using an MVC but somehow i cant get the combobox to work. Please help me,,
LoginController.java
package weltes.finance.controller;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
/**
* Created by engineering09 on 12/18/2015.
*/
public class LoginController {
#FXML private ComboBox userComboBox;
#FXML private void fillComboBox(){
userComboBox.getItems().addAll("Item1", "Item2");
}
}
loginscreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="163.0" prefWidth="357.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="weltes.finance.controller.LoginController">
<children>
<ComboBox fx:id="userComboBox" layoutX="15.0" layoutY="14.0" prefHeight="25.0" prefWidth="328.0" promptText="Select Registered User" />
<PasswordField layoutX="15.0" layoutY="56.0" prefHeight="25.0" prefWidth="328.0" promptText="Enter Password" />
<Button layoutX="15.0" layoutY="97.0" mnemonicParsing="false" prefHeight="51.0" prefWidth="328.0" text="Button" />
</children>
</AnchorPane>
First list the items in the FXML.
<ComboBox fx:id="userComboBox" editable="false" layoutX="14.0" layoutY="22.0" onAction="#setTimeMultiplier">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ComboBox>
Then add or remove items from the ObservableArrayList.
userComboBox.getItems().removeAll(comboTime.getItems());
userComboBox.getItems().addAll(...);

LoadException with FXML created with Scene Builder

I've generated FXML in Scene Builder:
<?xml version="1.0" encoding="UTF-8"?>
<?scenebuilder-preview-i18n-resource ../lang/ru_RU.properties?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" fx:id="mainStagePane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="UI.MainStageController">
<children>
<SplitPane dividerPositions="0.6596244131455399" focusTraversable="true" prefHeight="-1.0" prefWidth="1280.0" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="160.0">
<items>
<AnchorPane id="rootPane" fx:id="pane3d" prefHeight="930.0" prefWidth="1280.0" />
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Accordion id="accordeon" fx:id="accordion" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<panes>
<TitledPane animated="true" text="%3dSceneSettings" fx:id="x2">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Slider fx:id="farClipSlider" layoutY="50.0" majorTickUnit="10.0" max="100.0" min="-100.0" minorTickCount="1" prefWidth="337.0" showTickLabels="true" showTickMarks="true" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="75.0" />
<TextField layoutY="14.0" prefWidth="55.0" AnchorPane.rightAnchor="217.0" />
<Slider fx:id="distanceSlider" blockIncrement="25.0" layoutX="14.0" layoutY="133.0" majorTickUnit="50.0" max="500.0" min="-500.0" minorTickCount="25" prefWidth="399.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" />
<VBox layoutY="364.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0">
<children>
<HBox prefHeight="-1.0" prefWidth="-1.0">
<children>
<Label text="Label" />
<TextField prefWidth="55.0" />
</children>
</HBox>
<Slider fx:id="nearClipSlider" blockIncrement="1.0" majorTickUnit="10.0" max="100.0" min="-100.0" minorTickCount="1" prefWidth="338.0" showTickLabels="true" showTickMarks="true" snapToTicks="true" value="0.0" />
</children>
</VBox>
<Label layoutX="104.0" layoutY="17.0" text="Label" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="true" expanded="false" text="%3dModelSettings" fx:id="x1">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
</panes>
</Accordion>
</children>
</AnchorPane>
</items>
</SplitPane>
<ToolBar maxHeight="30.0" minHeight="30.0" prefHeight="30.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<items>
<ProgressBar fx:id="loadProgressBar" disable="false" prefWidth="200.0" progress="0.0" visible="true" />
</items>
</ToolBar>
<TabPane prefHeight="160.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="%file">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button alignment="CENTER" graphicTextGap="4.0" mnemonicParsing="false" prefHeight="90.0" prefWidth="90.0" text="%openFile" textAlignment="CENTER" textOverrun="ELLIPSIS" underline="false" wrapText="true" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="14.0" />
<Separator layoutX="170.0" orientation="VERTICAL" prefHeight="200.0" AnchorPane.bottomAnchor="10.0" AnchorPane.topAnchor="10.0" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="%analysis">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button mnemonicParsing="false" prefHeight="90.0" prefWidth="90.0" text="Button" AnchorPane.leftAnchor="14.0" AnchorPane.topAnchor="14.0" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
But suddenly I received the following exception:
javafx.fxml.LoadException:
/D:/Projects/CastAnalytics/out/production/CastAnalytics/UI/MainStage.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2592)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2570)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2416)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3160)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3121)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3094)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3070)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3062)
at core.CastAnalytics.start(CastAnalytics.java:32)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at UI.MainStageController.set(MainStageController.java:98)
at UI.MainStageController.initialize(MainStageController.java:34)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2523)
... 18 more
What does it mean and how can I fix it?
Well, you've got a null pointer at line 98 of MainStageController....
If I had to guess, I'd say that one of the #FXML variables you've defined in your Controller isn't named the same as in your FXML file, so you're getting a NPE.
You'd have to paste the relevant MainStageController.java code for anyone to know more.
Please post the offending line or SSCE if possible. I agree with jhsheets and your approach double checking the variable names against your fx:id names. I have encountered this exact error/situation myself at exactly the same place (in the initialize method trying to do some post-loading initialization) where a small typo in my fx:I'd caused an NPE. But it would be good to know what is going on in the set method where the NPE originates.

Resources