We're currently developing a social media platform as a project at school (to be handed in in 2 weeks), and we've ran into some issues when trying to get the UI to scale with the application window. Currently it works as supposed when previewing it in SceneBuilder (see the first link) , but it seems to be stuck in the upper left corner when running the actual application via IntelliJ IDEA. I've also linked a screenshot of how it's actual behavior is (second link).
This is how the scaling should work, and as you can see it does work when i use SceneBuilder's preview function.
This is how the scaling is working when we run the app, which isn't scaling at all.
At the moment we're just trying to understand whats wrong, therefore our only focus is the 'discover page' of the platform (you access the page from the events page). So i will post the fxml code for the discover page as well as the events controller. Bare in mind this is my first ever post on here, so if there's anything else that could help you help us just tell me and i'll get it uploaded! Here is a screenshot of all the classes in our project, if there's any other class that would be helpful if i post just say the word :)
All classes in the project.
FXML for the Discover page:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<BorderPane fx:id="borderPaneDiscover" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="JavaFX.DiscoverController">
<center>
<AnchorPane prefHeight="200.0" prefWidth="900.0" BorderPane.alignment="CENTER">
<children>
<TableView fx:id="tableViewDiscover" layoutY="62.0" onMouseClicked="#showDetails" prefHeight="331.0" prefWidth="900.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="65.0">
<columns>
<TableColumn fx:id="eventNameColumnDiscover" minWidth="180.0" prefWidth="75.0" text="Event name" />
<TableColumn fx:id="eventLocationColumnDiscover" minWidth="180.0" prefWidth="75.0" text="Event location" />
<TableColumn fx:id="eventDateColumnDiscover" minWidth="180.0" prefWidth="75.0" text="Event date" />
<TableColumn fx:id="eventCategoryColumnCategory" minWidth="180.0" prefWidth="75.0" text="Event category" />
<TableColumn fx:id="eventCreatorColumnDiscover" minWidth="180.0" prefWidth="75.0" text="Event Creator" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<Label alignment="CENTER" prefHeight="18.0" prefWidth="190.0" text="Choose category of the event:" textAlignment="CENTER" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" />
<ChoiceBox fx:id="choiceBoxDiscover" layoutX="375.0" layoutY="20.0" prefHeight="30.0" prefWidth="150.0" AnchorPane.leftAnchor="375.0" AnchorPane.rightAnchor="375.0" AnchorPane.topAnchor="20.0" />
<Button fx:id="buttonGo" layoutX="505.0" mnemonicParsing="false" onAction="#showTableDiscover" prefHeight="30.0" prefWidth="55.0" style="-fx-background-color: #313131; -fx-text-fill: #FFF; -fx-font-weight: BOLD;" text="Go" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="20.0">
<effect>
<DropShadow />
</effect>
</Button>
</children>
</AnchorPane>
</center>
<bottom>
<AnchorPane prefHeight="145.0" prefWidth="900.0" BorderPane.alignment="CENTER">
<children>
<TextArea fx:id="textAreaDescription" editable="false" layoutX="60.0" layoutY="20.0" prefHeight="120.0" prefWidth="250.0" wrapText="true" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="60.0" AnchorPane.rightAnchor="590.0" AnchorPane.topAnchor="20.0" />
<TextArea fx:id="textAreaParticipants" editable="false" layoutX="360.0" layoutY="20.0" prefHeight="120.0" prefWidth="250.0" wrapText="true" AnchorPane.bottomAnchor="10.0" AnchorPane.rightAnchor="290.0" AnchorPane.topAnchor="20.0" />
<Button fx:id="attendButton" layoutX="660.0" layoutY="45.0" mnemonicParsing="false" onAction="#attendEvent" prefHeight="50.0" prefWidth="100.0" style="-fx-background-color: #313131; -fx-text-fill: #ffffff; -fx-font-weight: BOLD;" text="Attend" AnchorPane.bottomAnchor="50.0" AnchorPane.rightAnchor="40.0" AnchorPane.topAnchor="45.0">
<effect>
<DropShadow />
</effect>
</Button>
<Text layoutX="65.0" layoutY="15.0" strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 14; -fx-font-weight: BOLD;" text="Description:" AnchorPane.leftAnchor="60.0" AnchorPane.topAnchor="2.0">
<font>
<Font name="Segoe UI Symbol" size="14.0" />
</font>
</Text>
<Text layoutX="365.0" layoutY="15.0" strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-size: 14; -fx-font-weight: BOLD;" text="Participants:" AnchorPane.rightAnchor="457.0" AnchorPane.topAnchor="2.0">
<font>
<Font name="Segoe UI Symbol" size="14.0" />
</font>
</Text>
</children>
</AnchorPane>
</bottom>
<top>
<AnchorPane prefHeight="16.0" prefWidth="900.0" BorderPane.alignment="CENTER">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="315.0" prefHeight="54.0" prefWidth="289.0" text="Discover events" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
<Button alignment="CENTER_RIGHT" layoutX="833.0" layoutY="10.0" mnemonicParsing="false" onAction="#BackToEvents" prefHeight="34.0" prefWidth="55.0" style="-fx-background-color: #313131; -fx-text-fill: #FFF; -fx-font-size: 14; -fx-font-weight: BOLD;" text="Back" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="10.0">
<font>
<Font name="Segoe UI Symbol" size="20.0" />
</font>
<effect>
<DropShadow />
</effect>
</Button>
</children>
</AnchorPane>
</top>
</BorderPane>
FXML for the events page:
package JavaFX;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import java.io.IOException;
public class EventsController {
private ObservableList<String> usersChoice = FXCollections.observableArrayList("My events(attended/to attend and created)", "Discover(search by category and attend)", "Create event");
#FXML
private BorderPane eventsPane;
#FXML
private ChoiceBox <String> choicebox;
#FXML
private Label labelOfUserChoice;
#FXML
private void initialize(){
choicebox.setItems(usersChoice);
//choicebox.getSelectionModel().selectedItemProperty().addListener(changeListener);
}
#FXML
private void changeLabel() throws IOException/*, InterruptedException*/{
switch (choicebox.getValue()) {
//ask sokol about it
case ("My events(attended/to attend and created)"):
labelOfUserChoice.setText("You have chosen: " + choicebox.getValue());
choicebox.setDisable(true);
//TimeUnit.SECONDS.sleep(5);
loadMyEvents();
break;
case ("Discover(search by category and attend)"):
labelOfUserChoice.setText("You have chosen: " + choicebox.getValue());
choicebox.setDisable(true);
//TimeUnit.SECONDS.sleep(1);
loadDiscover();
break;
case ("Create event"):
labelOfUserChoice.setText("You have chosen: " + choicebox.getValue());
//TimeUnit.SECONDS.sleep(1);
loadCreateEvent();
break;
default:
System.out.println("YOU'VE BETTER LEARN HOW TO USE JFX!");
}
}
#FXML
private void goToMainMenu(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/resources/mainMenu.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
#FXML
private void goToProfile(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/resources/profile.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
#FXML
private void goToFriends(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/resources/friendsPage.fxml"));
loader.setLocation(getClass().getResource("/resources/friendsPage.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
#FXML
private void goToChat(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/chatclient/FXMLDocument.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
private void loadMyEvents() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/resources/myEvents.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
private void loadDiscover() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/resources/discover.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
private void loadCreateEvent() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/resources/createEvent.fxml"));
BorderPane pane = loader.load();
eventsPane.getChildren().setAll(pane);
}
}
So far i've read probably all the posts in here that are issues remotely like ours, but none of them have seemed to work. I haven't been able to find a post stating the exact same issue as ours, where the preview window in SceneBuilder works as supposed and the program when we run it doesn't. I hope someone can help us out, thanks in advance! :)
Related
This is my main inside application package:
package application;
import java.net.URL;
import javax.swing.JOptionPane;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.Model;
public class Main extends Application {
Parent root;
Scene scene;
#Override
public void start(Stage primaryStage) {
try {
URL url = getClass().getResource("/view/AddCustomer.fxml");
root = FXMLLoader.load(url);
scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(e -> {
int save = JOptionPane.showConfirmDialog(null, "Do you want to save changes in the product table?");
if (save == 0) {
Model.getInstance().SaveCustomers();
Model.getInstance().SaveSessions();
Model.getInstance().SaveAdministrators();
Model.getInstance().SaveCoachs();
}
System.exit(0);
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
This is my addCustomerController inside controller package:
package controller;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
public class AddCustomerController {
#FXML
private Button cancelMemberButton;
#FXML
private Pane customerPane;
#FXML
private RadioButton dailyMemberType;
#FXML
private TextField memberDiscountField;
#FXML
private RadioButton memberFemaleButton;
#FXML
private TextField memberIdField;
#FXML
private RadioButton memberMaleButton;
#FXML
private TextField memberMobileField;
#FXML
private TextField memberNameField;
#FXML
private TextField memberNationalityField;
#FXML
private RadioButton monthlyMemberType;
#FXML
private Button saveMemberButton;
#FXML
private RadioButton yearlyMemberType;
#FXML
void cancelButtonClick(ActionEvent event) {
}
#FXML
void saveButtonClick(ActionEvent event) {
}
}
This is the FXML file inside view package(Its called AddCustomer.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ToggleButtonGroup?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<Pane fx:id="customerPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="532.0" prefWidth="789.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AddCustomerController">
<children>
<Label alignment="CENTER" contentDisplay="CENTER" layoutX="258.0" layoutY="34.0" prefHeight="41.0" prefWidth="274.0" text="Add Customer" textFill="#860b0b">
<font>
<Font name="Arial" size="37.0" />
</font>
</Label>
<Separator layoutY="98.0" prefHeight="1.0" prefWidth="789.0" style="-fx-background-color: #860b0b;">
<opaqueInsets>
<Insets />
</opaqueInsets>
</Separator>
<Label layoutX="27.0" layoutY="144.0" prefHeight="23.0" prefWidth="103.0" text="Member ID" />
<Label layoutX="27.0" layoutY="195.0" prefHeight="23.0" prefWidth="103.0" text="Name" />
<Label layoutX="27.0" layoutY="243.0" prefHeight="23.0" prefWidth="103.0" text="Mobile" />
<Label layoutX="27.0" layoutY="296.0" prefHeight="23.0" prefWidth="103.0" text="Nationality" />
<Label layoutX="27.0" layoutY="345.0" prefHeight="23.0" prefWidth="103.0" text="Gender" />
<Label layoutX="27.0" layoutY="394.0" prefHeight="23.0" prefWidth="103.0" text="MemberShip Type" />
<Label layoutX="27.0" layoutY="440.0" prefHeight="23.0" prefWidth="103.0" text="Discount" />
<TextField fx:id="memberIdField" layoutX="246.0" layoutY="143.0" prefHeight="30.0" prefWidth="486.0" />
<TextField fx:id="memberNameField" layoutX="246.0" layoutY="194.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberMobileField" layoutX="246.0" layoutY="242.0" prefHeight="25.0" prefWidth="486.0" />
<TextField fx:id="memberNationality" layoutX="246.0" layoutY="295.0" prefHeight="25.0" prefWidth="486.0" />
<RadioButton fx:id="memberMaleButton" layoutX="341.0" layoutY="348.0" mnemonicParsing="false" text="Male" />
<RadioButton fx:id="memberFemaleButton" layoutX="412.0" layoutY="348.0" mnemonicParsing="false" text="Female" />
<RadioButton fx:id="dailyMemberType" layoutX="341.0" layoutY="397.0" mnemonicParsing="false" text="Daily" />
<RadioButton fx:id="monthlyMemberType" layoutX="412.0" layoutY="397.0" mnemonicParsing="false" text="Monthly" />
<RadioButton fx:id="yearlyMemberType" layoutX="499.0" layoutY="397.0" mnemonicParsing="false" text="Yearly" />
<TextField fx:id="discountField" editable="false" layoutX="246.0" layoutY="439.0" prefHeight="25.0" prefWidth="486.0" />
<Button fx:id="saveMemberButton" layoutX="246.0" layoutY="482.0" mnemonicParsing="false" onAction="#saveButtonClick" prefHeight="30.0" prefWidth="164.0" text="Save" />
<Button fx:id="cancelMemberButton" layoutX="499.0" layoutY="482.0" mnemonicParsing="false" onAction="#cancelButtonClick" prefHeight="30.0" prefWidth="164.0" text="Cancel" />
<ToggleButtonGroup layoutX="130.0" layoutY="170.0" selectionType="SINGLE" />
</children>
</Pane>
This is the error I get when run my main:
javafx.fxml.LoadException:
/C:/Users/ADMIN/eclipse-workspace/Project_V2_V2/bin/view/AddCustomer.fxml
at javafx.fxml#19/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml#19/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2949)
at javafx.fxml#19/javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2793)
at javafx.fxml#19/javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2758)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2624)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml#19/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at Project_V2_V2/application.Main.start(Main.java:22)
at javafx.graphics#19/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#19/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics#19/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics#19/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#19/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.ClassNotFoundException: com.gluonhq.charm.glisten.control.ToggleButtonGroup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:3017)
at javafx.fxml#19/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:3006)
at javafx.fxml#19/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2947)
... 20 more
I searched online for problems, i found all of the issues that has been offered online are not similar to mine at least from what i understood. I fixed some things that were in my code. However, it still did not solve the issue for me. Please i need help fixing this issue.
You are using Gluons com.gluonhq.charm.glisten.control.ToggleButtonGroup from their mobile tooling. Do you really want to do that? Maybe using the standard JavaFX ToggleGroup would be sufficient. If you really want to use Gluons lib then you have to add this implementation 'com.gluonhq:charm-glisten:6.2.2' to your dependencies. But note, that glisten is a proprietary offering with a different license than JavaFX.
I'm running an FXML project and I can't load my fxml files, IntelliJ keeps indicating there is something wrong with my AnchorPane tag, however this was all generated by scene builder. This error even shows up for fxml files created by IntelliJ. The issue seems to be xmlns="http://javafx.com/javafx/19" within the AnchorPane tag at least that's where the compiler is pointing to.
I'm running IntelliJ 2022 with JDK 17.0.4.1 with JavaFX 19 using Maven, and I'm using scene builder 19.
here is my fxml code
`
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.LoginController">
<children>
<VBox prefHeight="400.0" prefWidth="600.0" />
<TextField id="Username" layoutX="115.0" layoutY="60.0" prefHeight="25.0" prefWidth="126.0" promptText="user#account.com" />
<Label layoutX="39.0" layoutY="64.0" prefHeight="17.0" prefWidth="63.0" text="Username" />
<Label layoutX="39.0" layoutY="116.0" text="Password" />
<TextField id="password" layoutX="115.0" layoutY="112.0" prefHeight="25.0" prefWidth="126.0" />
<Button id="login" layoutX="132.0" layoutY="210.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="85.0" text="Login" />
<Label layoutX="39.0" layoutY="153.0" prefHeight="17.0" prefWidth="63.0" text="Location" />
<TextField id="Location" disable="true" layoutX="115.0" layoutY="149.0" prefHeight="25.0" prefWidth="126.0" promptText="Your location here" />
<Label id="ErrorMessage" alignment="CENTER" disable="true" layoutX="91.0" layoutY="183.0" prefHeight="17.0" prefWidth="187.0" text="Error Label" textAlignment="CENTER" textFill="#e10724">
<font>
<Font name="System Italic" size="12.0" />
</font>
</Label>
</children>
</AnchorPane>
`
and here is my java code
`
package controllers;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class LoginController {
#FXML
private TextField Username;
//Buttons
#FXML
private TextField Password;
#FXML
private TextField Location;
#FXML
private Label ErrorMessage;
#FXML
private Button Login;
public void BeginStart(){
//Loader
try {
//debugging fxml path
URL uri = getClass().getResource("/views/login.fxml");
System.out.println(uri);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/views/login.fxml"));
Scene scene = new Scene(fxmlLoader.load());
Stage stage = new Stage();
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
/*Parent root = loader.load();
Stage stage = new Stage();
stage.setTitle("Scheduler Login");
stage.setScene(new Scene(root));
stage.show();*/
}catch(Exception e){
System.out.println("Failed to load login.fxml");
System.out.println("Exception: " + e.toString());
}
}
}
`
uri is non-null so there appears to be no problem with the file path.
Previously I've had the "uri not registered" error before I deleted the javaFX files included with Maven, and used a local copy of the JavaFX19 library, along with the proper JVM arguments.
Note: the image I included is from a previous version of my project. Under resources "controllers" is actually supposed to be "views".
Ok, so the issue was with the fx:controller reference. My controller class was not within src/main/java/com.example.scheduler.
After moving it there the error ceased.
Im trying to set the Text of a Textfield in my MainController from another file.
I read that this:
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
MainController mc = (MainController) loader.getController();
is how you're supposed to get a reference to the Controller Instance in Javafx. I tried to implement it, but it gives me a NullPointerException when trying to set the TextField.
Here is my Code:
Main.java:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Main extends Application {
static MainController mc;
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root,348,212);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Flextime calculator");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("assets/icon.png")));
primaryStage.setResizable(false);
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
mc = (MainController) loader.getController();
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public static MainController getMc() {
return mc;
}
}
MainController.java:
public class MainController {
#FXML Label fxFlextime;
#FXML Label fxTotalFlextime;
#FXML TextField fxStarttime;
#FXML TextField fxEndtime;
#FXML TextField fxDailyWorktime;
#FXML TextField fxBreaktime;
#FXML TextField fxAddBreaktime;
#FXML TextField fxBreakGap;
#FXML CheckBox fxAddBreak;
//Create Model, Userconfig
Model model = new Model();
UserConfig config = new UserConfig();
public void initialize() {
config.initialize();
}
}
UserConfig.java:
public class UserConfig {
public void initialize() {
insertDefault();
}
void insertDefault() {
List<String> prefValues = getPreference();
//FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
//MainController mc = (MainController) loader.getController();
// ^Tried also putting it here to see wether im referencing the variable wrong or something,
// so maybe im getting the Controller wrong? With mc.fxStarttime.setText("") it also didnt work here
Main.mc.fxStarttime.setText("");
Main.mc.fxEndtime.setText(prefValues.get(2));
Main.mc.fxDailyWorktime.setText(prefValues.get(3));
Main.mc.fxBreaktime.setText(prefValues.get(4));
Main.mc.fxAddBreaktime.setText(prefValues.get(5));
Main.mc.fxBreakGap.setText(prefValues.get(6));
}
static List<String> getPreference() {
// This will define a node in which the preferences can be stored
Preferences userPrefs = Preferences.userNodeForPackage(Main.class);
try {
String[] keys = userPrefs.keys();
if (keys == null) {
userPrefs.put("DEFStarttime" , "1");
userPrefs.put("DEFEndtime" , "");
userPrefs.put("DEFDailyWorktime", "");
userPrefs.put("DEFBreaktime" , "");
userPrefs.put("DEFAddBreaktime" , "");
userPrefs.put("DEFBreakGap" , "");
}
} catch (BackingStoreException ex) {
System.err.println(ex);
}
List<String> prefValues = new ArrayList<String>();
prefValues.add(userPrefs.get("DEFStarttime" , ""));
prefValues.add(userPrefs.get("DEFEndtime" , ""));
prefValues.add(userPrefs.get("DEFDailyWorktime" , ""));
prefValues.add(userPrefs.get("DEFBreaktime" , ""));
prefValues.add(userPrefs.get("DEFAddBreaktime" , ""));
prefValues.add(userPrefs.get("DEFBreakGap" , ""));
/*
for(String string : prefValues) {
System.out.println(string);
}
*/
return prefValues;
}
}
The Error:
javafx.fxml.LoadException:
/C:/eclipse-workspace/project/bin/application/main.fxml
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at application.Main.start(Main.java:18)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
... 17 more
Caused by: java.lang.NullPointerException
at application.UserConfig.insertDefault(UserConfig.java:25)
at application.UserConfig.initialize(UserConfig.java:18)
at application.MainController.initialize(MainController.java:39)
... 28 more
I tried to cut it down to the minimal amount of code.
Edit:
Here the Main.fxml:
<?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.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="212.0" prefWidth="348.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<MenuBar fx:id="fxMenuBar">
<menus>
<Menu fx:id="fxFileMenu" mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="fxExitItem" mnemonicParsing="false" onAction="#exitProgram" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Settings">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<FlowPane alignment="CENTER_LEFT" prefHeight="25.0" prefWidth="403.0">
<children>
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Starttime:" underline="true">
<FlowPane.margin>
<Insets left="10.0" top="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxStarttime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00:00">
<FlowPane.margin>
<Insets top="10.0" />
</FlowPane.margin></TextField>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="112.0" text="Required daily hours:" underline="true">
<FlowPane.margin>
<Insets left="10.0" right="5.0" top="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxDailyWorktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00.00">
<FlowPane.margin>
<Insets top="10.0" />
</FlowPane.margin></TextField>
</children>
</FlowPane>
<FlowPane alignment="CENTER_LEFT" prefHeight="36.0" prefWidth="300.0">
<children>
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Endtime:" underline="true">
<FlowPane.margin>
<Insets left="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxEndtime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00:00" />
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Breaktime:" underline="true">
<FlowPane.margin>
<Insets left="57.0" right="5.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxBreaktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00" />
</children>
</FlowPane>
<FlowPane prefHeight="38.0" prefWidth="300.0">
<children>
<CheckBox fx:id="fxAddBreak" mnemonicParsing="false" text="Add:">
<FlowPane.margin>
<Insets left="20.0" top="5.0" />
</FlowPane.margin>
</CheckBox>
<TextField fx:id="fxAddBreaktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00">
<FlowPane.margin>
<Insets left="10.0" right="5.0" top="5.0" />
</FlowPane.margin>
</TextField>
<Label text="breaktime from ">
<FlowPane.margin>
<Insets top="5.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxBreakGap" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00.00">
<FlowPane.margin>
<Insets top="5.0" />
</FlowPane.margin>
</TextField>
<Label text="working hours">
<FlowPane.margin>
<Insets left="5.0" top="5.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="32.0" prefWidth="300.0">
<children>
<Label text="Flextime:">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
<Label fx:id="fxFlextime" alignment="CENTER" prefHeight="10.0" prefWidth="35.0" text="0.00" textAlignment="CENTER">
<FlowPane.margin>
<Insets />
</FlowPane.margin>
</Label>
<Label text="h">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="32.0" prefWidth="300.0">
<children>
<Label text="Total flextime:" underline="true">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
<Label fx:id="fxTotalFlextime" alignment="CENTER" prefHeight="10.0" prefWidth="35.0" text="0.00" textAlignment="CENTER">
<FlowPane.margin>
<Insets />
</FlowPane.margin>
</Label>
<Label text="h">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="38.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#calculate" text="Calculate" underline="true">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Button>
<Button mnemonicParsing="false" onAction="#addToTotal" text="Add to total" />
<Button mnemonicParsing="false" onAction="#clear" text="Clear">
<FlowPane.margin>
<Insets left="10.0" right="10.0" />
</FlowPane.margin>
</Button>
</children>
</FlowPane>
</children>
</VBox>
The reason why you are getting the NullPointerException is because the Main.mc is null at the point where you are trying to access it fields in UserConfig.
In the line where you load your main.fxml Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); the initialize() method in the controller MainController is invoked and at that point you are invoking userConfig.initialize() which also make a backward reference to Main.mc which at that point is null.
You can make the config public in your MainController class and initialize user config after you set the mc field in the Main class. Or you can inject the MainController as parameter into the UserConfig initialize method.
Main.java
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent) loader.load();
Scene scene = new Scene(root,348,212);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Flextime calculator");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("assets/icon.png")));
primaryStage.setResizable(false);
mc = (MainController) loader.getController();
mc.config.initialize();
MainController.java
public UserConfig config = new UserConfig();
#FXML
public void initialize() {
//remove the config.initialize(); from here
}
Getting LoadExeption evertime i try to run this code.
the code ran fine the first time but after that i just keep getting this error.
Its just a simple login page with not much coding what so ever.
Main class below
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args); // tried changing this to string[0] didnt work
}
}
Main controller class below i wanted to add more sql stuff to this but i keep getting the same error.
In this i am just trying to change
package application;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import javafx.fxml.FXML;
public class MainController {
#FXML
private Label LblStatus;
#FXML
private TextField TxtUsername;
#FXML
private TextField TxtPassword;
public void Login(ActionEvent event)
{
LblStatus.setText("Login sucsess");
}
}
login page
All of this was created using scene builder and youtube.
so i basically have no idea why i am getting this error.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.MainController">
<!-- TODO Add Nodes -->
<children>
<Pane prefHeight="400.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TitledPane animated="false" layoutX="0.0" layoutY="0.0" prefHeight="400.0000999999975" prefWidth="500.0" text="LOGIN FORM" textAlignment="CENTER" wrapText="false">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="TxtUsername" layoutX="227.0" layoutY="129.0" prefWidth="200.0" promptText="Enter Username" />
<Label layoutX="129.0" layoutY="129.0" prefWidth="98.0" text="USERNAME:">
<font>
<Font size="15.0" fx:id="x1" />
</font>
</Label>
<Label font="$x1" layoutX="129.0" layoutY="183.0" prefWidth="98.0" text="PASSWORD:" />
<Button font="$x1" layoutX="224.0" layoutY="225.0" mnemonicParsing="false" onAction="#Login" text="LOGIN" />
<PasswordField fx:id="TxtPassword" layoutX="224.0" layoutY="183.0" prefWidth="200.0" promptText="Enter Password" />
<Label id="Status" fx:id="LblStatus" layoutX="259.0" layoutY="69.0" text="Status">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
<font>
<Font name="Arial Black" size="15.0" />
</font>
</TitledPane>
</children>
</Pane>
</children>
</AnchorPane>
You have the wrong imports in your controller. Replace
import java.awt.Label;
with
import javafx.scene.control.Label ;
etc...
I'm building an application using JavaFx and scene builder, however everything works fine, except when I add the Controller class.
I get the following error:
Exception in Application start method Exception in thread "main"
java.lang.RuntimeException: Exception in Application start method at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at
com.sun.javafx.application.LauncherImpl$$Lambda$1/868693306.run(Unknown
Source) at java.lang.Thread.run(Thread.java:745) Caused by:
javafx.fxml.LoadException:
/C:/Users/M%20ROSE/Documents/Info%20Trivia/out/production/Info%20Trivia/sample/gameScene1.fxml:15
However the moment I remove the fx:controller attribute from this line in my fxml code it works perfectly.
<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
Here are the relevant codes
Controller Class
package sample;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.* ;
import javafx.geometry.* ;
import javafx.scene.text.* ;
import javafx.scene.image.* ;
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 ;
public class Controller {
//Initialize fxml controls
public Button trueButton;
public Button falseButton;
public Label playerLabel;
public Label questionLabel;
public Label scoreValue;
public ImageView questionImage;
public Rectangle redBar;
public Rectangle greenBar;
//Create array for level 1 questions
String[][] levelOneData = {
{"This is a Sequence Diagram","f", null},
{"This diagram is for a database","t", null},
{"This is a rack diagram","t", null},
{"This is a flow chart","f", null},
{"This is a kind of UML diagram","t", null}
};
Image[] levelOneImages = new Image[] {
new Image("res/images/l1q1.png"),
new Image("res/images/l1q2.png"),
new Image("res/images/l1q3.png"),
new Image("res/images/l1q4.png"),
new Image("res/images/l1q5.png")
};
public void levelOneInitializer(){
questionLabel.setText(levelOneData[0][0]);
questionImage.setImage(levelOneImages[0]);
System.out.println("done");
}
}
Main Class
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 {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("gameScene1.fxml"));
primaryStage.setTitle("Info Trivia");
primaryStage.setScene(new Scene(root, 800, 450));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
gameScene1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?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?>
<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<HBox styleClass="questheader" BorderPane.alignment="CENTER">
<children>
<ImageView pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#res/images/userICon.png" />
</image>
</ImageView>
<Label id="playerName" fx:id="playerLabel" text="Player 1" textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
<Region prefHeight="0.0" prefWidth="382.0" />
<Label id="ScoreLabel" layoutX="40.0" layoutY="10.0" text="Score: " textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
<Label id="scoreValue" fx:id="scoreValue" layoutX="129.0" layoutY="10.0" text="0" textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
</children>
<BorderPane.margin>
<Insets left="50.0" right="50.0" />
</BorderPane.margin>
<padding>
<Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
</padding>
</HBox>
</top>
<left>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="50.0" />
</BorderPane.margin>
<children>
<ImageView fx:id="questionImage" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#res/images/blankimage.png" />
</image>
<StackPane.margin>
<Insets left="35.0" />
</StackPane.margin>
</ImageView>
</children>
</StackPane>
</left>
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<Rectangle fx:id="greenBar" arcHeight="5.0" arcWidth="5.0" fill="#34b316" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="10.0" />
<Rectangle fx:id="redBar" arcHeight="5.0" arcWidth="5.0" fill="#a93535" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="620.0" />
</children>
<padding>
<Insets bottom="40.0" left="85.0" right="85.0" />
</padding>
</HBox>
</bottom>
<center>
<BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets right="85.0" />
</BorderPane.margin>
<top>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="questionLabel" text="This Is a Question" textFill="#191919">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
</children>
</StackPane>
</top>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="trueButton" mnemonicParsing="false" prefWidth="100.0" text="True">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Region prefWidth="200.0" />
<Button fx:id="falseButton" mnemonicParsing="false" prefWidth="100.0" text="False" />
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
<padding>
<Insets left="20.0" top="20.0" />
</padding>
</HBox>
</center>
</BorderPane>
</center>
</BorderPane>
You controller have to implement Initializable, Try with the following :
public class Controller implement Initializable{
//Initialize fxml controls
#FXML
public Button trueButton;
#FXML
public Button falseButton;
#FXML
public Label playerLabel;
#FXML
public Label questionLabel;
#FXML
public Label scoreValue;
#FXML
public ImageView questionImage;
#FXML
public Rectangle redBar;
#FXML
public Rectangle greenBar;
//Create array for level 1 questions
String[][] levelOneData = {
{"This is a Sequence Diagram","f", null},
{"This diagram is for a database","t", null},
{"This is a rack diagram","t", null},
{"This is a flow chart","f", null},
{"This is a kind of UML diagram","t", null}
};
Image[] levelOneImages = new Image[] {
new Image("res/images/l1q1.png"),
new Image("res/images/l1q2.png"),
new Image("res/images/l1q3.png"),
new Image("res/images/l1q4.png"),
new Image("res/images/l1q5.png")
};
public void levelOneInitializer(){
questionLabel.setText(levelOneData[0][0]);
questionImage.setImage(levelOneImages[0]);
System.out.println("done");
}
}