I have: 2 FXML Files, and 2 Controllers. Each FXML's have the controllers merged. This is the code opening the new FXML (While the real fxml is running it opens another one)
try {
Parent root = FXMLLoader.load(getClass().getResource(
"settings.fxml"));
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.centerOnScreen();
stage.show();
} catch (Exception e) {
e.printStackTrace();
Logger.logCrash("ApplicationScene", e);
}
This is the controller of the FXML file which gets opened
@FXML
public TextField destination;
@FXML
public TextArea view;
@FXML
public TextArea point;
public void initialize() {
destination.appendText("LOL");
view.appendText("LAA");
point.appendText("LOWKAPF");
}
As you can see, I'm appending text on all declared fields (FXML-ID'S ARE BOUND!) after the root has been loaded through the initialize method. Sounds good and great, but I get a NullPointerException.
To point the things out clearly: - I've already bound the fxml-ids to their corresponding components. - The FXML file gets loaded up correctly (root is being loaded correctly, else the initialize method wouldnt work)
This has nothing to do with the static access. Even without static access this does not work.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="373.0" prefWidth="518.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="removed">
<children>
<TextField fx:id="destination" layoutX="14.0" layoutY="19.0" prefHeight="25.0" prefWidth="464.0" promptText="Destination of the files" text="ie.: C:\" />
<Text layoutX="14.0" layoutY="57.0" strokeType="OUTSIDE" strokeWidth="0.0" text="moo" wrappingWidth="464.0" />
<TextArea fx:id="point" layoutX="14.0" layoutY="76.0" prefHeight="42.0" prefWidth="464.0" promptText="HI/>
<Text layoutX="14.0" layoutY="131.0" strokeType="OUTSIDE" strokeWidth="0.0" text="meow" wrappingWidth="464.0" />
<TextArea fx:id="view" layoutX="14.0" layoutY="135.0" prefHeight="42.0" prefWidth="464.0" promptText="HI" />
<Text layoutX="14.0" layoutY="191.0" strokeType="OUTSIDE" strokeWidth="0.0" text="m00" wrappingWidth="464.0" />
<Button layoutX="220.0" layoutY="269.0" mnemonicParsing="false" onAction="#initialize" text="Default" />
</children>
</AnchorPane>
Oh. Ok, so, What I did is was:
package com.engine.application.content;
import com.engine.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Settings extends Application {
public static void start() {
Application.launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(
"settings.fxml"));
Scene scene = new Scene(root);
setStageProperties(primaryStage, scene);
}
/**
* Sets the properties of the application
*
* @param stage
* the stage's properties to set
* @param scene
* the scene's properties to set
*/
private void setStageProperties(Stage stage, Scene scene) {
stage.setScene(scene);
stage.setTitle("Test");
stage.centerOnScreen();
stage.setResizable(true);
stage.show();
Logger.log("Launcher", "Set stage properties");
}
}
Then I'm calling
Application.start()
when a button is clicked
This is the result:
Caused by: java.lang.IllegalStateException: Application launch must not be called more than once
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at javafx.application.Application.launch(Unknown Source)
at xxx.Settings.start(Settings.java:14)
at xxx.openMenu(ApplicationScene.java:43)
... 56 more
I'm not calling it somewhere else btw.
EDIT: This is the Settings application init.
public class Settings extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(
"settings.fxml"));
Scene scene = new Scene(root);
setStageProperties(primaryStage, scene);
System.out.println("YAY");
}
/**
* Sets the properties of the application
*
* @param stage
* the stage's properties to set
* @param scene
* the scene's properties to set
*/
private void setStageProperties(Stage stage, Scene scene) {
stage.setScene(scene);
stage.setTitle("Test");
stage.centerOnScreen();
stage.setResizable(true);
ApplicationScene.settingsMenu = stage;
Logger.log("Launcher", "Set stage properties");
}
}
This is the Main application public class ApplicationLauncher extends Application {
/**
* The main method
*
* @param args
* the arguments given upon start
*/
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Logger.log("Launcher", "Starting up..");
Parent root = FXMLLoader.load(getClass().getResource(
"ah.fxml"));
Directory directory = new Directory();
// Logger.logError("LOL", "ERROR! LOLOLOL L /n LOL \n LOL LOL");
Scene scene = new Scene(root);
directory.createFolder();
setStageProperties(stage, scene);
Settings.main(null);
Logger.log("Launcher", "Application started up!");
}
/**
* Sets the properties of the application
*
* @param stage
* the stage's properties to set
* @param scene
* the scene's properties to set
*/
private void setStageProperties(Stage stage, Scene scene) {
stage.setScene(scene);
stage.setTitle("Test");
stage.centerOnScreen();
stage.setResizable(true);
stage.show();
Logger.log("Launcher", "Set stage properties");
}
}
Result:
Caused by: java.lang.IllegalStateException: Application launch must not be called more than once
It's no where else called. (Btw. doing Settings.main(null); is the same as Settings.launch(); lol)
After re-thinking the concept
This did it:
new Settings().start(new Scene());