I am dealing with JavaFX , I am trying to get my graphical composant from my FXML page into the java classes, but it always return null value
Page.fxml
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<content>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" fx:id="Mycontent">
<top>
<fx:include source="TopMenu.fxml" />
</top>
<center>
<fx:include source="Operation.fxml" />
</center>
<left>
<fx:include source="SideBar_Inhumer.fxml" />
</left>
<bottom>
<fx:include source="Footer.fxml" />
</bottom>
</BorderPane>
</content>
</ScrollPane>
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
public BorderPane Mycontent;
#FXML
public void goToDemandeur(){
System.out.println(Mycontent);
//Mycontent.setCenter(FXMLLoader.load(getClass().getResource("Demandeur.fxml")));
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
My code always print "null"
if I try for example Mycontent.getCenter()it gives me this error
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
(which normal because Mycontent is null, but why it is null? )
PS: my method goToDemandeur() is called into an other fxml page SideBar_Inhumer.fxml after a click onMouseClicked="#goToDemandeur"
SideBar_Inhumer.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox layoutX="77.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Pane >
<children>
<Label alignment="CENTER" text="Inhumer" >
<font>
<Font size="24.0" />
</font>
<padding>
<Insets bottom="20.0" left="25.0" right="25.0" top="30.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #F08080;" onMouseClicked="#goToDemandeur">
<children>
<Label text="Demandeur" >
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</Pane>
<Pane style="-fx-background-color: #C6E2B9;">
<children>
<Label text="Defunt">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FBF6A5;">
<children>
<Label text="Emplacement">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FFCC99;">
<children>
<Label text="Prestataire">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #D8B46D;">
<children>
<Label text="Opération">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
</children>
</VBox>
Main.java
package View;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("../View/Page.fxml"));
primaryStage.setTitle("Finalys");
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
Scene scene =new Scene(root, 600, 475);
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(Main.class.getResource("bootstrap2.css").toExternalForm());
}
public static void main(String[] args) {
launch(args);
}
}
Related
I'm doing Internationalization on my JavaFX(11) application.
In resources.languages, I have multiple .properties files which are saved as UTF-8 character encoding.
But when I get with resourceBundle and pass them to fxml, they becomes squares like below.
Main.java
package application;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import jfxtras.styles.jmetro.JMetro;
import jfxtras.styles.jmetro.Style;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Locale locale = new Locale("my_MM");
ResourceBundle bundle = ResourceBundle.getBundle("resources.languages.langs", locale);
AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("Sample.fxml"), bundle);
Scene scene = new Scene(root);
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);
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="760.0" prefWidth="1200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/15.0.1" fx:controller="application.SampleController">
<children>
<VBox prefHeight="760.0" prefWidth="268.0" style="-fx-background-color: #3367D6;" styleClass="sidePanel">
<children>
<AnchorPane prefHeight="134.0" prefWidth="268.0">
<children>
<Label layoutX="41.0" layoutY="27.0" styleClass="title" text="Title" textFill="WHITE">
<font>
<Font size="33.0" />
</font>
</Label>
<Label layoutX="41.0" layoutY="68.0" styleClass="title" text="%interTitleDescription" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0" style="-fx-background-color: white;">
<children>
<Label alignment="TOP_LEFT" layoutX="36.0" layoutY="18.0" text="%interStudent" textFill="#3367d6">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="34.0" layoutY="17.0" text="%interTransactions" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interDonators" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interAdmins" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interSettings" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</AnchorPane>
langs_my_MM.properties
interTitleDescription = Management System
interStudent = ကျောင်းသားများ
interTransactions = အလှူငွေထုတ်မှတ်တမ်း
interDonators = အလှူရှင်များ
interAdmins = စီမံခန့်ခွဲသူများ
interSettings = Setting များ
I'm having this error while following a tutorial:
javafx.fxml.LoadException: BorderPane is not a valid type.
/C:/Users/Eduardo%20Abreu/Documents/Eclipse-Workspace/UnifacsProjeto/bin/projeto/resources/RootLayout.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2774)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at projeto.MainApp.initRootLayout(MainApp.java:33)
at projeto.MainApp.start(MainApp.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at projeto.MainApp.showFilmeOverview(MainApp.java:48)
at projeto.MainApp.start(MainApp.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application projeto.MainApp
Here's my code:
package projeto;
import java.io.IOException;
import ch.makery.address.MainApp;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("CineTudo");
initRootLayout();
showFilmeOverview();
}
public void initRootLayout(){
try {
//Carrega o layout root do arquivo fxml
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("resources/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene cena = new Scene(rootLayout);
primaryStage.setScene(cena);
primaryStage.show();
} catch(IOException e) {
e.printStackTrace();
}
}
public void showFilmeOverview() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation((MainApp.class.getResource("resources/FilmeOverview.fxml")));
AnchorPane filmeOverview = (AnchorPane) loader.load();
rootLayout.setCenter(filmeOverview);
}catch (IOException e){
e.printStackTrace();
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
Here's the tutorial code:
package ch.makery.address;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
initRootLayout();
showPersonOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
I don't have any id's on the Fxml file and the tutorial's one doesn't have it either, i created FilmeOverview.fxml as AnchorPane and RootLayout as BorderPane, I tried changing the position of the method but it doesn't work, does anyone know how to fix this?
Edit: Here's the FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?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.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.29797979797979796" layoutX="191.0" layoutY="120.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<TableView layoutX="-25.0" layoutY="46.0" prefHeight="398.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn prefWidth="75.0" text="Filme" />
<TableColumn prefWidth="75.0" text="Sala" />
<TableColumn prefWidth="75.0" text="Categoria" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Detalhes do Filme">
<font>
<Font name="Open Sans Light" size="15.0" />
</font>
</Label>
<GridPane layoutX="14.0" layoutY="49.0" prefHeight="268.0" prefWidth="547.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="49.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Título:">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label layoutX="20.0" layoutY="17.0" text="Sala:" GridPane.rowIndex="1">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label layoutX="20.0" layoutY="55.0" text="Categoria:" GridPane.rowIndex="2">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Diretor:" GridPane.rowIndex="3">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Duração:" GridPane.rowIndex="4">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Protagonista:" GridPane.rowIndex="5">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Classificação:" GridPane.rowIndex="6">
<font>
<Font size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Roboto" size="16.0" />
</font>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
<Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="6">
<font>
<Font name="Roboto" size="16.0" />
</font>
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
</Label>
</children>
</GridPane>
<ButtonBar layoutX="276.0" layoutY="425.0" prefHeight="59.0" prefWidth="267.0">
<buttons>
<JFXButton style="-fx-background-color: #00BCD4;" text="Adicionar" />
<JFXButton style="-fx-background-color: #03A9F4;" text="Editar" />
<JFXButton style="-fx-background-color: #F44336;" text="Deletar" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Edit 2: Here's rootLayout.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<BorderPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="800.0">
<menus>
<Menu mnemonicParsing="false" text="Arquivo">
<items>
<MenuItem mnemonicParsing="false" text="Fechar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Editar">
<items>
<MenuItem mnemonicParsing="false" text="Deletar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Ajuda">
<items>
<MenuItem mnemonicParsing="false" text="Sobre" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
This is not a duplicate, changing the import doesn't fix it, i still get
Caused by: javax.xml.stream.XMLStreamException: ParseError at
[row,col]:[31,3]
You have a couple of issues:
The FXML doesn't have an import statement for javafx.scene.layout.BorderPane.
Your rootLayout.fxml file is invalid FXML. You close the BorderPane element with an AnchorPane tag.
You define the contents of the border pane as children, where they are supposed to be defined by position.
Sample valid FXML with the above issues fixed:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<top>
<MenuBar layoutY="2.0" prefHeight="25.0" prefWidth="800.0">
<menus>
<Menu mnemonicParsing="false" text="Arquivo">
<items>
<MenuItem mnemonicParsing="false" text="Fechar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Editar">
<items>
<MenuItem mnemonicParsing="false" text="Deletar" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Ajuda">
<items>
<MenuItem mnemonicParsing="false" text="Sobre" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
As an aside, I recommend downloading Gluon SceneBuilder and using that to edit your FXML.
Also note, some IDEs, such as IntelliJ, include code completion, type checking and syntax checking which would highlight many of the above errors in your document when you load the file into the IDE.
I am having trouble displaying the combo box options in javafx and scene builder.
First I have tried to do use an array, which I guess does not work. I then tried to use an ObservableList<String> and create an instance of the combo box like
ComboBox combo = new ComboBox(list);
This still would not show the combobox options.
Controller class:
package javafxapplication1;
import javafxapplication1.JavaFXApplication1;
import java.net.URL;
import static java.util.Collections.list;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javax.lang.model.element.Element;
/**
*
* #author KJ4CC
*/
public class FXMLDocumentController implements Initializable {
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
#FXML
private Label label;
#FXML
private TextField dateText;
#FXML
private TextField time;
#FXML
private ComboBox band;
public void setTimeDate(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
dateText.setText(javaFXApp.getDate());
}
public void setTime(){
JavaFXApplication1 javaFXApp = new JavaFXApplication1();
time.setText(javaFXApp.getTime());
}
#Override
public void initialize(URL url, ResourceBundle rb) {
band = new ComboBox(options);
}
}
Main java class:
public class JavaFXApplication1 extends Application {
private FXMLDocumentController initScene;
private DateFormat dtf;
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public String getDate(){
dtf = new SimpleDateFormat("dd/MM/yy");
Date dateobj = new Date();
return dtf.format(dateobj);
}
public String getTime(){
dtf = new SimpleDateFormat("HH:mm;ss");
Date dateobj = new Date();
return dtf.format(dateobj);
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="383.0" prefWidth="590.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
<top>
<VBox prefHeight="235.0" prefWidth="543.0" BorderPane.alignment="CENTER">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<TableView prefHeight="210.0" prefWidth="634.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
<VBox.margin>
<Insets left="2.0" right="2.0" />
</VBox.margin>
</TableView>
</children>
</VBox>
</top>
<bottom>
<VBox prefHeight="160.0" prefWidth="676.0" BorderPane.alignment="CENTER">
<children>
<HBox prefHeight="22.0" prefWidth="625.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Date:" wrappingWidth="83.462890625">
<HBox.margin>
<Insets left="5.0" right="20.0" />
</HBox.margin>
</Text>
<Label onMouseClicked="#setTimeDate" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Time:" wrappingWidth="73.40625" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Call:" wrappingWidth="122.94921875">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Band:" wrappingWidth="58.443359375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Freq:" wrappingWidth="106.974609375" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Location:" />
</children>
</HBox>
<HBox prefHeight="35.0" prefWidth="625.0">
<children>
<TextField id="dateText" fx:id="dateText" prefHeight="25.0" prefWidth="94.0">
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
<TextField fx:id="time" layoutX="10.0" layoutY="10.0" prefHeight="25.0" prefWidth="71.0">
<padding>
<Insets left="5.0" />
</padding>
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</TextField>
<TextField layoutX="104.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<ComboBox fx:id="band" prefHeight="25.0" prefWidth="57.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</ComboBox>
<TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</TextField>
<TextField layoutX="306.0" layoutY="10.0" prefHeight="25.0" prefWidth="101.0" />
</children>
</HBox>
<HBox prefHeight="30.0" prefWidth="580.0">
<children>
<Text fill="#14bdd7" onMouseClicked="#setTimeDate" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Date">
<HBox.margin>
<Insets right="20.0" />
</HBox.margin>
</Text>
<Text fill="#14bdd7" layoutX="10.0" layoutY="23.0" onMouseClicked="#setTime" strokeType="OUTSIDE" strokeWidth="0.0" text="Use Current Time" />
</children>
</HBox>
</children>
<BorderPane.margin>
<Insets left="5.0" right="5.0" />
</BorderPane.margin>
</VBox>
</bottom>
</BorderPane>
Any help would be appreciated! I am kind of new to this scene builder stuff and using javafx. I am just feeling my way around it. Thanks for the help!
Ok so i figured out the problem. I was creating a new instance of a combo box thus creating an empty one. Here is the method with the revised working code.
public void initialize(URL url, ResourceBundle rb) {
ObservableList<String> options =
FXCollections.observableArrayList(
"Option 1",
"Option 2",
"Option 3"
);
band.setItems(options);
}
So I have created a port scanner in Java.
While using Swing, everything works smoothly with 0 errors.
I however tried to convert my app to a JavaFX style of GUI, and nothing works when I press the buttons (Even though I have set the method ID's in the GUI builder).
What seems to be my problem?
My code:
package networkTools.gui;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class PortScannerController implements Initializable {
#FXML
TextField hostName;
#FXML
TextField fromPort;
#FXML
TextArea log;
#FXML
Button scan;
#FXML
Button reset;
#FXML
Label label;
#Override
public void initialize(URL url, ResourceBundle rb) {
hostName.requestFocus();
}
public void initFocus() {
hostName.requestFocus();
}
#FXML
private void onScan() {
int fp;
String h;
Socket s;
if (hostName.getText().equals("")) {
log.setText("Fill everything correct.");
return;
} else if (fromPort.getText().equals("")) {
log.setText("Fill everything correct..");
return;
} else if (!fromPort.getText().matches("[0-9]*")) {
log.setText("Give a number for a port ");
return;
}
// scan.disable(false);
reset.setText("Stop");
log.setText("");
log.clear();
label.setText("");
h = hostName.getText();
fp = Integer.parseInt(fromPort.getText());
label.setText("Port " + fp + " being tested (max +- 15 sec.)");
try {
s = new Socket(h, fp);
log.appendText("Poort " + fp + " is open.\n");
log.clear();
s.close();
} catch (Exception er) {
log.appendText("Poort " + fp + " is closed");
}
// scan.setEnabled(true);
reset.setText("Reset");
label.setText("Press scan to start.");
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="networkTools.gui.PortScannerController">
<children>
<StackPane prefHeight="80.0" prefWidth="600.0">
<children>
<Label id="title" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="50.0" prefWidth="400.0" text="Port Scanner" StackPane.alignment="CENTER">
<font>
<Font name="Arial Black" size="18.0" />
</font>
</Label>
</children>
</StackPane>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="131.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="407.0" minWidth="10.0" prefWidth="269.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="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="100.0" text="Host Name">
<font>
<Font name="Arial Black" size="12.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="100.0" text="Port" GridPane.rowIndex="1">
<font>
<Font name="Arial Black" size="12.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<TextField fx:id="fromPort" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Arial Black" size="12.0" />
</font>
</TextField>
<ToggleButton id="commandbutton" fx:id="onScan" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Scan" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<font>
<Font name="Arial Black" size="12.0" />
</font>
</ToggleButton>
<ToggleButton id="commandbutton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Reset" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<font>
<Font name="Arial Black" size="12.0" />
</font>
</ToggleButton>
<TextField fx:id="hostName" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Arial Black" size="12.0" />
</font>
</TextField>
</children>
</GridPane>
<TextArea id="textpane" fx:id="log" editable="false" prefHeight="148.0" prefWidth="600.0">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextArea>
<StackPane prefHeight="80.0" prefWidth="380.0" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="10.0" left="20.0" />
</VBox.margin>
<children>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="200.0" text="Press Scan to start" textFill="#1815c9">
<font>
<Font name="Arial Black" size="13.0" />
</font>
</Label>
</children>
</StackPane>
</children>
</VBox>
There is nowhere that you associate the handler with the button. You need an onAction="#onScan" in the FXML for the "Scan" button:
<ToggleButton id="commandbutton" onAction="#onScan" fx:id="onScan" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Scan" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
There are several other things that look incorrect in your FXML:
You have several elements with the same CSS id ("commandButton" for example); you should use a styleClass instead of an id if you want to share style among several components.
You declare an fx:id="onScan" for the button above, but there is no field in your controller called onScan.
It's not really clear why you are using a ToggleButton instead of a regular Button for what appears to be an action. ToggleButtons are generally intended for toggling between two different states, not issuing a command/action.
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");
}
}