I tried creating my own custom node, but I miserably failed somehow.
It always throws me an error when I try to run the controller/scene.
This is my class representing the custom node
package com.lollookup.scene.customcontrol;
import com.lollookup.scene.data.ChampionInfoData;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javax.xml.soap.Text;
import java.io.IOException;
/**
* #author Yasin
*/
public class ChampionInfo extends Pane {
#FXML
private ImageView championImage;
#FXML
private Text KDA;
#FXML
private Text winRate;
#FXML
private Text masteryScore;
#FXML
private Text masteryLevel;
public ChampionInfo() {
try {
Parent root = FXMLLoader.load(getClass().getResource("championinfo.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void setData(ChampionInfoData championInfoData) {
this.championImage.setImage(new Image(championInfoData.getUrl()));
this.KDA.setTextContent(championInfoData.getKDA());
this.winRate.setTextContent(championInfoData.getWinRate());
this.masteryScore.setTextContent(championInfoData.getMasteryScore());
this.masteryLevel.setTextContent(championInfoData.getMasteryLevel());
}
}
And this is my fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="84.0" prefWidth="238.0" type="Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.lollookup.scene.customcontrol.ChampionInfo">
<children>
<Separator layoutY="3.0" prefHeight="0.0" prefWidth="238.0" />
<HBox prefHeight="84.0" prefWidth="200.0">
<children>
<ImageView fx:id="championImage" fitHeight="72.0" fitWidth="66.0" pickOnBounds="true" preserveRatio="true">
<HBox.margin>
<Insets left="10.0" top="10.0" />
</HBox.margin>
</ImageView>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<Text fx:id="KDA" strokeType="OUTSIDE" strokeWidth="0.0" text="kda">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</Text>
<Text fx:id="winRate" strokeType="OUTSIDE" strokeWidth="0.0" text="winRate" />
<Text fx:id="masteryLevel" strokeType="OUTSIDE" strokeWidth="0.0" text="champLevel" />
<Text fx:id="masteryScore" strokeType="OUTSIDE" strokeWidth="0.0" text="champScore" />
</children>
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</VBox>
</children>
</HBox>
<Separator layoutY="79.0" prefHeight="0.0" prefWidth="238.0" />
</children>
</fx:root>
My problem now is that somehow I can't really start/implement the custom node.
So no matter if I create a Scene like this:
package com.lollookup.scene.customcontrol;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* #author Yasin
*/
public class ChampionInfoExample extends Application {
#Override
public void start(Stage stage) throws Exception {
//ChampionInfo championInfo = new ChampionInfo(new ChampionInfoData("https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150", "1:1", "50%", "0", "1"));
ChampionInfo championInfo = new ChampionInfo();
//championInfo.setData(new ChampionInfoData("https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150", "1:1", "50%", "0", "1"));
stage.setScene(new Scene(championInfo));
stage.setTitle("Custom Control");
stage.setWidth(300);
stage.setHeight(200);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
or instantiate it like that:
Stream.of(championData).forEach(p -> championDataContainer.getChildren().add(new ChampionInfo()));
This is what is being thrown:
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:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.StackOverflowError
at java.net.URLStreamHandler.setURL(URLStreamHandler.java:537)
at java.net.URLStreamHandler.parseURL(URLStreamHandler.java:304)
at sun.net.www.protocol.file.Handler.parseURL(Handler.java:67)
at java.net.URL.<init>(URL.java:615)
at java.net.URL.<init>(URL.java:483)
at sun.misc.URLClassPath$FileLoader.getResource(URLClassPath.java:1222)
at sun.misc.URLClassPath$FileLoader.findResource(URLClassPath.java:1212)
at sun.misc.URLClassPath$1.next(URLClassPath.java:240)
at sun.misc.URLClassPath$1.hasMoreElements(URLClassPath.java:250)
at java.net.URLClassLoader$3$1.run(URLClassLoader.java:601)
at java.net.URLClassLoader$3$1.run(URLClassLoader.java:599)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader$3.next(URLClassLoader.java:598)
at java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623)
at sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45)
at sun.misc.CompoundEnumeration.hasMoreElements(CompoundEnumeration.java:54)
at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:354)
at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474)
at javax.xml.stream.FactoryFinder$1.run(FactoryFinder.java:352)
at java.security.AccessController.doPrivileged(Native Method)
at javax.xml.stream.FactoryFinder.findServiceProvider(FactoryFinder.java:341)
at javax.xml.stream.FactoryFinder.find(FactoryFinder.java:313)
at javax.xml.stream.FactoryFinder.find(FactoryFinder.java:227)
at javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:154)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2472)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at com.lollookup.scene.customcontrol.ChampionInfo.<init>(ChampionInfo.java:38)
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
What should I do?
I'd appreciate any kind of assistance
Thanks
Edit:
After debugging, I kinda found out why it was null. Had nothing to do with the codes given. Thank you StackOverflow!
Related
I've realised only through clearing up code that I thought I could clear up and had cleared from some of my Controller classes where my issue is. To word my question better and provide more clarity to the concept of prioritisation I'll give a brief explanation followed by some code. Imagine an interface with a side of the screen being consistent and permanent to the interface where you have Buttons for menu-ing where you essentially move between tabs changing the rest of the Stage passing fxml files to the Pane associated.
Reading examples and questions I understood this to be viable and using the framework from this link https://gist.github.com/jewelsea/6460130 seeing a header remain consistent while switching Scene between vista1.fxml and vista2.fxml, my understanding was that if the Header remains from main.fxml as part of the scene that the Stage is therefore using either both main.fxml and vista1.fxml or main.fxml and vista2.fxml concurrently and thus both associated controllers exist and will have access to corresponding methods according to the Pane interacted with.
That is clearly not the case though and the consistent Pane in this case main.fxml does not have its controller since when the other is loaded and creates its controller it replaces or clears the other controller in some form or another I now presume is the case. I thought I could avoid code duplication by using separate Panes with the consistency of the menu bar but also being able to use its buttons.
Some code as an example:
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
stage.setTitle("Vista Viewer");
stage.setScene(createScene(loadMainPane()));
stage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(
VistaNavigator.MAIN));
MainController mainController = loader.getController();
VistaNavigator.setMainController(mainController);
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
return scene;
}
public static void main(String[] args) {
launch(args);
}
}
VistaNavigator.java
package sample;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
public class VistaNavigator {
public static final String MAIN = "main.fxml";
public static final String VISTA_1 = "vista1.fxml";
public static final String VISTA_2 = "vista2.fxml";
public static final String OTHER_MENU_OPTION = "otherMenu.fxml";
private static MainController mainController;
public static void setMainController(MainController mainController) {
VistaNavigator.mainController = mainController;
}
public static void loadVista(String fxml) {
try {
mainController.setVista(
FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
MainController.java
package sample;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
public class MainController {
#FXML
private StackPane vistaHolder;
public void setVista(Node node) {
vistaHolder.getChildren().setAll(node);
}
public void homebtn() throws Exception{
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
public void otherMenuOption() throws Exception{
VistaNavigator.loadVista(VistaNavigator.OTHER_MENU_OPTION);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.MainController">
<children>
<VBox prefWidth="155.0">
<children>
<Label fx:id="headerLabel" maxWidth="120.0" text="Header" />
</children>
</VBox>
<Pane layoutY="40.0" prefHeight="200.0" prefWidth="120.0" style="-fx-background-color: #091D34;">
<children>
<Button fx:id="button1" layoutY="60.0" mnemonicParsing="false" onAction="#homebtn" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="Home" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button fx:id="button2" layoutY="110.0" mnemonicParsing="false" onAction="#otherMenuOption" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="OtherMenuOption" textFill="WHITE">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</Pane>
<StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
</children>
</AnchorPane>
otherMenu.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="otherMenuOption" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller">
<children>
<Label fx:id="label" layoutX="135.0" layoutY="57.0" maxWidth="120.0" text="other program stuff" VBox.vgrow="NEVER" />
</children>
</StackPane>
Vista1Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista1Controller {
#FXML
void nextPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}
vista1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista1" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#nextPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista2" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Vista2Controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class Vista2Controller {
#FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
}
vista2.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista2Controller" prefHeight="400.0" prefWidth="600.0">
<children>
<Pane fx:id="Vista2" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
<Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#previousPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista1" textFill="#ffffff">
<font>
<Font name="Britannic Bold" size="20.0" />
</font>
</Button>
</Pane>
</children>
</AnchorPane>
Is there a good way to maintain this design and prevent code duplication within the specific controllers and would it be through inheritance or passing parameters, I'm not sure what this is comparable to nor whether it's possible?
I have a application which uses two JFXDatePickers from JFoenix. They work perfectly when running from Eclipse but do not work when I export the program to a JAR file. They still return the text that is set by default but when I try to select a date using it the window freezes and the application does not respond.
I've searched trough stackoverflow and javafx and jfoenix documentation but can't seem to find an answer. Can someone please help me? This is for a school project. Any suggestions?
#FXML
private JFXDatePicker startDatePicker;
#FXML
private JFXDatePicker endDatePicker;
#FXML
private void initialize() {
this.endDatePicker.setValue(LocalDate.now());
this.startDatePicker.setValue(LocalDate.now().withDayOfMonth(1));
}
Thank you very much!
Edit: Thank you for the suggestion, Slaw. I've launched it trough the command line and didn't get any exceptions. The controller class where the error happens is this one:
package application;
import java.time.LocalDate;
import com.jfoenix.controls.JFXDatePicker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
public class SeeAllQuotesFilterScreenController {
#FXML
private Button cancelButton;
#FXML
private Button enterButton;
#FXML
private JFXDatePicker startDatePicker;
#FXML
private JFXDatePicker endDatePicker;
#FXML
private void initialize() {
this.endDatePicker.setValue(LocalDate.now());
this.startDatePicker.setValue(LocalDate.now().withDayOfMonth(1));
}
#FXML
void cancelButtonClicked(ActionEvent event) {
new Alert(Alert.AlertType.INFORMATION, "changes to a different screen on the complete application").showAndWait();
}
#FXML
void enterButtonClicked(ActionEvent event) {
new Alert(Alert.AlertType.INFORMATION, "changes to a different screen on the complete application").showAndWait();
}
}
Also, the fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXDatePicker?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<BorderPane style="-fx-background-color: #E2FAFE;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SeeAllQuotesFilterScreenController">
<center>
<BorderPane prefHeight="500.0" prefWidth="500.0" BorderPane.alignment="CENTER">
<center>
<VBox alignment="TOP_CENTER">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Please choose the search criteria below.">
<VBox.margin>
<Insets left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</Text>
<HBox alignment="CENTER">
<children>
<JFXDatePicker fx:id="startDatePicker" promptText="Start Date">
<HBox.margin>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</HBox.margin>
</JFXDatePicker>
<JFXDatePicker fx:id="endDatePicker" promptText="End Date">
<HBox.margin>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</HBox.margin>
</JFXDatePicker>
</children>
<padding>
<Insets bottom="40.0" top="40.0" />
</padding>
</HBox>
<JFXButton fx:id="enterButton" onAction="#enterButtonClicked" text="Enter">
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" />
</VBox.margin></JFXButton>
</children>
<padding>
<Insets bottom="3.0" left="5.0" right="3.0" top="30.0" />
</padding>
</VBox>
</center>
</BorderPane>
</center>
<top>
<HBox alignment="CENTER" prefHeight="37.0" prefWidth="500.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment="CENTER_LEFT" HBox.hgrow="NEVER">
<children>
<JFXButton fx:id="cancelButton" onAction="#cancelButtonClicked" prefHeight="25.0" prefWidth="54.0" text="Cancel" />
</children>
</HBox>
<HBox>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Search Criteria">
<font>
<Font size="18.0" />
</font>
<HBox.margin>
<Insets bottom="12.0" left="12.0" right="12.0" top="12.0" />
</HBox.margin>
</Text>
</children>
</HBox>
</children>
</HBox>
</top>
</BorderPane>
My Main():
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane) FXMLLoader.load(getClass().getResource("SeeAllQuotesFilterScreen.fxml"));
Scene scene = new Scene(root, 500, 600);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initialize() {
}
public static void main(String[] args) {
launch(args);
}
}
I appreciate all and any help!
Okay, I've been working through some issues with this program and I think I've finally gotten it to a point where I understand what is wrong. I'm trying to follow this tutorial a bit: http://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm But my program has the add a row on a different FXML page than the Table View is on. I think the program is having trouble connecting the two. I've looked in to trying to find ways to make them talk to each other (put everything in one Controller and it didn't like it, tried passing the controller through the class and that didn't work {might have done it wrong though}). My program also has Integers and Doubles in it which are not covered in that tutorial so I've tried to figure those out on my own (probably better ways of doing it than I did).
But right now I'm just focused on figuring out why it keeps thinking
data = partTable.getItems();
Is null (line 77 in AddPartController). Any help or other FXML/JavaFX tutorials would be greatly appreciated (though I've already looked through a lot of them).
FXMLDocument.fxml (main page)
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import fxmltableview.*?>
<?import ims.Part?>
<?import ims.Inhouse?>
<?import ims.Outsourced?>
<BorderPane id="main" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ims.FXMLDocumentController" >
<top>
<Label fx:id="mainTitle" text="Inventory Management System" />
</top>
<center>
<HBox fx:id="holding">
<children>
<VBox styleClass="contentBox">
<children>
<HBox styleClass="topBox">
<HBox styleClass="subHeading">
<Label text="Parts" />
</HBox>
<HBox styleClass="searchBox">
<Button text="Search" />
<TextField />
</HBox>
</HBox>
<TableView fx:id="partTable" styleClass="dataTable">
<columns>
<TableColumn text="Part ID">
<cellValueFactory>
<PropertyValueFactory property="id" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="nameColumn" text="Part Name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Inventory Level">
<cellValueFactory>
<PropertyValueFactory property="instock" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Price/Cost per Unit">
<cellValueFactory>
<PropertyValueFactory property="price" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Inhouse name="Part 1" price="5.00" instock="5" max="10" min="1" />
<Inhouse name="Part 2" price="7.00" instock="2" max="11" min="2" />
</FXCollections>
</items>
<sortOrder>
<fx:reference source="nameColumn" />
</sortOrder>
</TableView>
<HBox styleClass="modificationButtons">
<children>
<Button onAction="#addPart" text="Add" />
<Button onAction="#modifyPart" text="Modify" />
<Button text="Delete" />
</children>
</HBox>
</children>
</VBox>
<VBox styleClass="contentBox">
<children>
<HBox styleClass="topBox">
<HBox styleClass="subHeading">
<Label text="Products" />
</HBox>
<HBox styleClass="searchBox">
<Button text="Search" />
<TextField />
</HBox>
</HBox>
<TableView fx:id="productTable" styleClass="dataTable">
<columns>
<TableColumn text="Part ID" />
<TableColumn text="Part Name" />
<TableColumn text="Inventory Level" />
<TableColumn text="Price/Cost per Unit" />
</columns>
</TableView>
<HBox styleClass="modificationButtons">
<children>
<Button onAction="#addProduct" text="Add" />
<Button onAction="#modifyProduct" text="Modify" />
<Button text="Delete" />
</children>
</HBox>
</children>
</VBox>
</children>
</HBox>
</center>
<bottom>
<HBox fx:id="exitButton">
<children>
<Button onAction="#closeProgram" text="Exit" />
</children>
</HBox>
</bottom>
</BorderPane>
FXMLDocumentController
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ims;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
/**
*
* #author chelseacamper
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void addPart(ActionEvent event) throws IOException {
Parent add_part_parent = FXMLLoader.load(getClass().getResource("addPart.fxml"));
Scene add_part_scene = new Scene(add_part_parent);
add_part_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(add_part_scene);
app_stage.show();
}
#FXML
private void modifyPart(ActionEvent event) throws IOException {
Parent modify_part_parent = FXMLLoader.load(getClass().getResource("modifyPart.fxml"));
Scene modify_part_scene = new Scene(modify_part_parent);
modify_part_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(modify_part_scene);
app_stage.show();
}
#FXML
private void addProduct(ActionEvent event) throws IOException {
Parent add_product_parent = FXMLLoader.load(getClass().getResource("addProduct.fxml"));
Scene add_product_scene = new Scene(add_product_parent);
add_product_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(add_product_scene);
app_stage.show();
}
#FXML
private void modifyProduct(ActionEvent event) throws IOException {
Parent modify_product_parent = FXMLLoader.load(getClass().getResource("modifyProduct.fxml"));
Scene modify_product_scene = new Scene(modify_product_parent);
modify_product_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(modify_product_scene);
app_stage.show();
}
#FXML
private void closeProgram(ActionEvent event) throws IOException {
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.close();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
addPart.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane id="addPage" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ims.AddPartController">
<fx:define>
<ToggleGroup fx:id="inOutGroup" />
</fx:define>
<center>
<VBox fx:id="verticalHolding">
<children>
<HBox fx:id="topRow">
<Label text="Add Part"/>
<RadioButton fx:id="inhouse" toggleGroup="$inOutGroup" text="In-House"/>
<RadioButton fx:id="outsourced" toggleGroup="$inOutGroup" selected="true" text="Outsourced"/>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="ID" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField promptText="Auto Gen - Disabled" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Name" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="partNameField" promptText="Part Name" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Inv" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="partInstockField" promptText="Inv" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Price/Cost" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="partPriceField" promptText="Price/Cost" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label text="Max" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField styleClass="smallTextField" fx:id="partMaxField" promptText="Max" />
<Label text="Min" />
<TextField styleClass="smallTextField" fx:id="partMinField" promptText="Min" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
<Label fx:id="inhouseLabel" text="Machine ID" />
<Label fx:id="outsourcedLabel" text="Company Name" />
</HBox>
<HBox styleClass="halfWidthRight">
<TextField fx:id="inhouseTextField" promptText="Mach ID" />
<TextField fx:id="outsourcedTextField" promptText="Comp Nm" />
</HBox>
</HBox>
<HBox styleClass="fullWidth">
<HBox styleClass="halfWidthLeft">
</HBox>
<HBox styleClass="halfWidthRight">
<Button onAction="#addInhouse" text="Save" />
<Button onAction="#backToMain" text="Cancel" />
</HBox>
</HBox>
</children>
</VBox>
</center>
</BorderPane>
AddPartController
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ims;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* #author chelseacamper
*/
public class AddPartController implements Initializable {
#FXML
ToggleButton inhouse;
#FXML
ToggleButton outsourced;
#FXML
Label inhouseLabel;
#FXML
Label outsourcedLabel;
#FXML
TextField inhouseTextField;
#FXML
TextField outsourcedTextField;
#FXML
private TableView<Inhouse> partTable;
#FXML
private TextField partNameField;
#FXML
private TextField partInstockField;
#FXML
private TextField partPriceField;
#FXML
private TextField partMaxField;
#FXML
private TextField partMinField;
/**
* Initializes the controller class.
* #param url
* #param rb
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
inhouseLabel.visibleProperty().bind( inhouse.selectedProperty() );
outsourcedLabel.visibleProperty().bind( outsourced.selectedProperty() );
inhouseTextField.visibleProperty().bind( inhouse.selectedProperty() );
outsourcedTextField.visibleProperty().bind( outsourced.selectedProperty() );
inhouseLabel.managedProperty().bind( inhouse.selectedProperty() );
outsourcedLabel.managedProperty().bind( outsourced.selectedProperty() );
inhouseTextField.managedProperty().bind( inhouse.selectedProperty() );
outsourcedTextField.managedProperty().bind( outsourced.selectedProperty() );
}
#FXML
public void addInhouse(ActionEvent event){
ObservableList<Inhouse> data;
data = partTable.getItems();
data.add(new Inhouse(partNameField.getText(),
Integer.parseInt(partInstockField.getText()),
Double.parseDouble(partPriceField.getText()),
Integer.parseInt(partMaxField.getText()),
Integer.parseInt(partMinField.getText()),
Integer.parseInt(inhouseTextField.getText())
// Integer.parseInt(outsourcedTextField.getText())
));
partNameField.setText("");
partInstockField.setText(String.valueOf(partInstockField));
partPriceField.setText(String.valueOf(partPriceField));
partMaxField.setText(String.valueOf(partMaxField));
partMinField.setText(String.valueOf(partMinField));
inhouseTextField.setText(String.valueOf(inhouseTextField));
// outsourcedTextField.setText(String.valueOf(outsourcedTextField));
}
#FXML
private void backToMain(ActionEvent event) throws IOException {
Parent add_main_parent = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene add_main_scene = new Scene(add_main_parent);
add_main_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(add_main_scene);
app_stage.show();
}
}
Errors that you get when you click add and then Save (don't even have to enter stuff)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
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.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8390)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.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$MouseHandler.process(Scene.java:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
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$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$294/109927940.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
Caused by: 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:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
... 46 more
Caused by: java.lang.NullPointerException
at ims.AddPartController.addInhouse(AddPartController.java:77)
... 56 more
The is no element in addPart.fxml with fx:id="partTable". Consequently partTable is null, and
partTable.getItems();
throws a null pointer exception.
You need to inject partTable into the controller for the FXML in which it is defined:
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private TableView<Inhouse> partTable ;
// ...
}
The AddPartController only needs access to the list of items associated with the table, so you can define a field for it, and a method for initializing it:
public class AddPartController implements Initializable {
// ...
private ObservableList<Inhouse> tableItems ;
public void setTableItems(ObservableList<Inhouse> tableItems) {
this.tableItems = tableItems ;
}
// ...
}
Then set the items when you load addPart.fxml:
#FXML
private void addPart(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("addPart.fxml"));
Parent add_part_parent = loader.load();
AddPartController addPartController = loader.getController();
addPartController.setTableItems(partTable.getItems());
Scene add_part_scene = new Scene(add_part_parent);
add_part_scene.getStylesheets().add("style.css");
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.setScene(add_part_scene);
app_stage.show();
}
and then of course you just need
#FXML
public void addInhouse(ActionEvent event){
tableItems.add(new Inhouse(partNameField.getText(),
Integer.parseInt(partInstockField.getText()),
Double.parseDouble(partPriceField.getText()),
Integer.parseInt(partMaxField.getText()),
Integer.parseInt(partMinField.getText()),
Integer.parseInt(inhouseTextField.getText())
// Integer.parseInt(outsourcedTextField.getText())
));
}
(FWIW I have no idea what
partInstockField.setText(String.valueOf(partInstockField));
etc etc is supposed to do.)
Good evening guys,
I am still at the very beginning of Java programming. My current target is to resize the top of a BorderPane during the launch of the initialization. For sure, it would be very beneficial to have a separate class that contains the static constants (like the value for the size).
Unfortunately, I'm always getting a NullPointerException. I gues it's something really simple that I don't see. I hope that you can help me.
Thanks a lot in advance!
package main;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#FXML
Pane main_top;
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("MainGui.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setMaximized(true);
BorderPane.layoutInArea(main_top, 0, 0, 200, GlobalProperties.MainGuiTopPaneHeight, 0, null, false, false, null, null, false);
} catch(Exception e) {
e.printStackTrace();
}
}
EDIT:
java.lang.NullPointerException
at javafx.scene.layout.Region.layoutInArea(Unknown Source)
at main.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/2117598489.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1401292544.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
EDIT2 (MainGui.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MainGuiController">
<top>
<Pane fx:id="main_top" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525260;" BorderPane.alignment="CENTER" />
</top>
<left>
<Pane fx:id="main_left" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525252;" BorderPane.alignment="CENTER" />
</left>
<center>
<Pane fx:id="main_center" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
<right>
<Pane fx:id="main_right" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525252;" BorderPane.alignment="CENTER" />
</right>
<bottom>
<Pane fx:id="main_bottom" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #525260;" BorderPane.alignment="CENTER" />
</bottom>
</BorderPane>
Finally after some days of trial and error, I found a solution. Now, after it's solved it looks pretty easy ;-)
All I had to do was adding the controller initialization interface in the controller.
package main;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
public class MainGuiController implements Initializable{
//***** #author n3wton
//**** FXML declarations for MainGui.fxml
#FXML
private Pane main_top;
#FXML
private Button button1;
//***** Beginning of auto-initialized method
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
button1.setPrefSize(1, 1);
main_top.setPrefSize(50, 10);
}
}
When I click on submit button on Login page homepage should be displayed but I get this error:
EXCEPTION
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1768)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
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.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Node.fireEvent(Node.java:8175)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
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:745)
Caused by: 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:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
... 47 more
Caused by: java.lang.NullPointerException
at com.emo.Navigator.showLoginPage(Navigator.java:61)
at com.emo.view.LoginController.authenticate(LoginController.java:37)
... 57 more
MAIN APP
package com.emo;
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;
import com.emo.view.HomePageController;
import com.emo.view.LoginController;
public class MainApp extends Application {
/**
* Constructor
*/
#Override
public void start(Stage primaryStage) {
Navigator nav = new Navigator();
nav.primaryStage = primaryStage;
primaryStage.setTitle("EMO APP");
nav.initRootLayout();
nav.showLoginPage();
}
public static void main(String[] args) {
launch(args);
}
}
Login Controller
package com.emo.view;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import com.emo.MainApp;
import com.emo.Navigator;
public class LoginController {
#FXML
private TextField usernameText;
#FXML
private PasswordField passwordText;
#FXML
private Label statusLabel;
int attempt = 0;
private MainApp mainApp;
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
/*
* #FXML void nextPane() { VistaNavigator.loadVista(VistaNavigator.VISTA_2);
* }
*/
#FXML
public void authenticate() {
if (attempt < 3) {
if (usernameText.getText().equals("abc")
&& passwordText.getText().equals("abc")) {
statusLabel.setText("");
Navigator nav = new Navigator();
nav.showLoginPage();
} else {
attempt++;
statusLabel.setText("Invalid Credentials");
}
} else {
statusLabel.setText("Your account has been locked");
}
}
}
HomePage Controller
package com.emo.view;
import javafx.fxml.FXML;
import com.emo.MainApp;
public class HomePageController {
// Reference to the main application.
private MainApp mainApp;
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
#FXML
public void emo() {
}
#FXML
public void cashManagement() {
}
#FXML
public void reports() {
}
#FXML
public void userProfile() {
}
#FXML
public void email() {
}
}
Navigator
package com.emo;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import com.emo.view.HomePageController;
import com.emo.view.LoginController;
public class Navigator {
Stage primaryStage;
BorderPane rootLayout=null;
FXMLLoader loader = new FXMLLoader();
AnchorPane pane = null;
/**
* Returns the main stage.
*
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
/**
* 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 Login Page inside the root layout.
*/
public void showLoginPage() {
try {
// Load person overview.
loader.setLocation(MainApp.class
.getResource("view/eMO_SignIn.fxml"));
pane = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(pane);
// Give the controller access to the main app.
LoginController controller = loader.getController();
controller.setMainApp(new MainApp());
} catch (IOException e) {
e.printStackTrace();
}
}
public void showHomePage() {
try {
pane = null;
// Load person overview.
loader.setLocation(MainApp.class
.getResource("view/eMO_HomePage.fxml"));
pane = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(pane);
// Give the controller access to the main app.
HomePageController controller = loader.getController();
controller.setMainApp(new MainApp());
} catch (IOException e) {
e.printStackTrace();
}
}
}
eMO_Homepage.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="250.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.emo.view.HomePageController">
<children>
<ImageView fitHeight="49.0" fitWidth="250.0" layoutY="-2.0" pickOnBounds="true">
<image>
<Image url="#../../../../../../Desktop/thCA60O0D6.jpg" />
</image>
</ImageView>
<Label layoutX="27.0" layoutY="14.0" text="Home Page" textFill="#a12121">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<ImageView fitHeight="354.0" fitWidth="250.0" layoutY="46.0" pickOnBounds="true">
<image>
<Image url="#../../../../../../Desktop/thCA7ZWC4O.jpg" />
</image>
</ImageView>
<Button layoutX="117.0" layoutY="10.0" mnemonicParsing="false" text="Sync" />
<VBox layoutX="2.0" layoutY="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="50.0">
<children>
<Button id="emo" layoutY="-1.0" mnemonicParsing="false" onAction="#emo" prefHeight="57.0" prefWidth="256.0" text="EMO">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button id="cashManagement" layoutY="56.0" mnemonicParsing="false" onAction="#cashManagement" prefHeight="57.0" prefWidth="256.0" text="Cash Management">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button id="reports" layoutY="113.0" mnemonicParsing="false" onAction="#reports" prefHeight="57.0" prefWidth="256.0" text="Reports">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button id="userProfile" layoutY="170.0" mnemonicParsing="false" onAction="#userProfile" prefHeight="57.0" prefWidth="256.0" text="User Profile">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Button id="email" layoutY="227.0" mnemonicParsing="false" onAction="#email" prefHeight="57.0" prefWidth="256.0" text="Email">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
</children>
</VBox>
</children>
</AnchorPane>
eMO_SignIn.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="250.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.emo.view.LoginController">
<children>
<ImageView fitHeight="49.0" fitWidth="250.0" layoutY="-2.0" pickOnBounds="true">
<image>
<Image url="#../../../../Images/Grey.jpg" />
</image>
</ImageView>
<Label layoutX="27.0" layoutY="14.0" text="Sign In" textFill="#a12121">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<ImageView fitHeight="354.0" fitWidth="250.0" layoutY="46.0" pickOnBounds="true">
<image>
<Image url="#../../../../Images/Red.jpg" />
</image>
</ImageView>
<VBox layoutX="27.0" layoutY="79.0" spacing="5.0" AnchorPane.leftAnchor="15.0">
<children>
<Label layoutX="27.0" layoutY="79.0" text="Username:-*" textFill="#f8e4e4">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<TextField fx:id="usernameText" layoutX="27.0" layoutY="105.0" prefHeight="42.0" prefWidth="150.0" />
<Label layoutX="28.0" layoutY="148.0" text="Password:-*" textFill="#f8e4e4">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<PasswordField fx:id="passwordText" layoutX="28.0" layoutY="174.0" prefHeight="42.0" prefWidth="150.0" />
</children>
</VBox>
<Button layoutX="125.0" layoutY="236.0" mnemonicParsing="false" onAction="#authenticate" prefHeight="25.0" prefWidth="68.0" text="Sign In" AnchorPane.leftAnchor="100.0" AnchorPane.rightAnchor="82.0">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<Label fx:id="statusLabel" alignment="CENTER" layoutX="46.0" layoutY="294.0" prefHeight="17.0" prefWidth="164.0" AnchorPane.bottomAnchor="100.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="280.0" />
</children>
</AnchorPane>
RootLayout.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="400.0" prefWidth="600.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<top>
<MenuBar BorderPane.alignment="CENTER">
<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>
</top>
</BorderPane>
In LoginController.authenticate(), you do:
Navigator nav = new Navigator();
nav.showLoginPage();
and in Navigator.showLoginPage() you have:
pane = loader.load();
rootLayout.setCenter(pane);
However, at no point was rootLayout initialized for this Navigator instance, so you get the NullPointerException as shown in the stack trace.
One possible fix would be to give the LoginController a reference to the Navigator you have already created (and properly initialized), so it doesn't have to create a new one:
public class LoginController {
private Navigator navigator ;
public void setNavigator(navigator) {
this.navigator = navigator ;
}
// everything else as before...
#FXML
public void authenticate() {
if (attempt < 3) {
if (usernameText.getText().equals("abc")
&& passwordText.getText().equals("abc")) {
statusLabel.setText("");
navigator.showLoginPage();
} else {
attempt++;
statusLabel.setText("Invalid Credentials");
}
} else {
statusLabel.setText("Your account has been locked");
}
}
}
and then
public class Navigator {
// everything else as before....
public void showLoginPage() {
try {
// Load person overview.
loader.setLocation(MainApp.class
.getResource("view/eMO_SignIn.fxml"));
pane = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(pane);
// Give the controller access to the main app.
LoginController controller = loader.getController();
controller.setMainApp(new MainApp());
// Give the controller access to this navigator:
controller.setNavigator(this);
} catch (IOException e) {
e.printStackTrace();
}
}
}