JavaFX Use one Loader load two different nodes - javafx

Everyone.I would like to ask, Can we use one loader to load two different FXMLs at one time? Actually, I want to know whether can I load two fxmls, so that these two fxmls can be displayed at the same time, at one screen.
public class NodeLink extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
AnchorPane root = (AnchorPane)loader.load(getClass().getResource("StartPanel.fxml"));
Group centralNode = (Group)loader.load(this.getClass().getResource("CentralNode.fxm."));
root.getChildren().setAll(centralNode);
//Group centralNode = FXMLLoader.load(getClass().getResource("CentralNode.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
As above, Group centralNode = ... this statement will cause error, as follows:
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)...
So, why this will cause errors? Can we use several fxmls once to make up a screen?
Thank you so much!

Related

JavaFX java.lang.IllegalStateException: Location is not set

Help! I'm stuck.. I try to run my main javafx app
Here is my codes;
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/com/utmkl/fxml/SimulatorDisplay.fxml"));
Parent content = (Parent)loader.load();
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setScene(new Scene(content));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
Below is my folder structure:
picture Folder structure
Below is the error
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.utmkl.VMCSManager.start(VMCSManager.java:43)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
I've found the answer..
the resource need to be in the same folder structure as main class
Here is my Maven-JavaFX Folder structure
VMCS
- src/main/java
- com.utmkl
VMCSManager.java
- src/main/resources
- com.utmkl.fxml
SimulatorDisplay.fxml
So, the correct code (which success to run)
VMCSManager.java
#Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
Parent content = loader.load();
Scene scene = new Scene(content);
primaryStage.setResizable(false);
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
Even though you do set a location with loader.setLocation(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); it is likely that the resource URL doesn't actually refer to an existing resource on the classpath.
If you add System.out.println(getClass().getResource("/com/utmkl/fxml/ControllerDisplay.fxml")); to your code I think it will print null as a result.

JavaFX NullPointerException Button in other class

I've to create an application for a school project. After you login, you should go to the dashboard. That works, but, it throws a NullPointerException when I try to set the button do disabled.
In this file, the stage is changing (after logging in):
public class ScreenController {
public void setScene(Stage stage, Parent root,Button button, String file){
if(file == "dashboard"){
stage = (Stage) button.getScene().getWindow();
try {
root = FXMLLoader.load(getClass().getResource("Dashboard.fxml"));
} catch (IOException ex) {
System.out.println("IOException: "+ex);
}
Scene scene = new Scene(root);
stage.setTitle("Corendon - Dashboard");
stage.setScene(scene);
stage.show();
Dashboard dashboard = new Dashboard();
}
}
}
And on the last line, the button should be set to disabled...
Dashboard dashboard = new Dashboard();
... by this class:
public class Dashboard extends ScreenController {
#FXML public Button buttonDashboard1;
public Dashboard(){
buttonDashboard1.setDisable(true);
}
}
But that is not working it throws the NullPointerException:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at
...
Caused by: java.lang.NullPointerException
at fastenyourseatbelts.Dashboard.<init>(Dashboard.java:11)
at fastenyourseatbelts.ScreenController.setScene(ScreenController.java:33)
at fastenyourseatbelts.LoginController.buttonLogin(LoginController.java:74)
... 59 more
I'm trying for hours now, but I don't get the solution... Does anyone know what is going wrong and why?
Move the code from the constructor of your controller to the initialize. This method is invoked by the FXMLLoader, after injection of all fields is completed and therefore should have access to the buttonDashboard1 instance:
public class Dashboard extends ScreenController {
#FXML public Button buttonDashboard1;
#FXML
private void initialize() {
buttonDashboard1.setDisable(true);
}
}
You also have to make sure the controller is either specified in the root element of the fxml file e.g. (replace packagename with the package of the Dashboard class)
<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="packagename.Dashboard">
<children>
<Button text="click me" fx:id="buttonDashboard1"/>
</children>
</VBox>
or set as controller for the fxml before loading it:
FXMLLoader loader = new FXMLLoader(getClass().getResource("Dashboard.fxml"));
Dashboard dashboard = new Dashboard();
loader.setController(dashboard);
root = loader.load();
(don't use a fx:controller attribute in this case)

FXML Doccument refusing to import other fxml files

I have a main FXML document for my program which contains a TabPane. For each tab I want it to have its own controller and fxml file. When I try to include the external fmxl files into the main fxml document, my program refuses to run. here is my main FXML document:
here is a copy of my java file
#Override
public void start(Stage stage) throws Exception {
FXMLLoader fxml = new FXMLLoader();
Parent root = fxml.load(getClass().getResource("FXMLDocument.fxml").openStream());
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
FXMLDocumentController fdc = fxml.getController();
}
Error:
Caused by: javafx.fxml.LoadException: Base location is undefined. unknown path:97
This error is caused because you have not set the location property of the FXMLLoader, and instead you are specifying an InputStream from which to load the FXML. I think the FXMLLoader must need to know the location of the original fxml file in order to resolve the location of the included file. You should really only use the load(InputStream) method in exceptional circumstances: when you are loading the fxml from a source other than a resource (i.e. file or resource in your application jar file).
Instead, use
FXMLLoader fxml = new FXMLLoader();
fxml.setLocation(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();
or, equivalently,
FXMLLoader fxml = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxml.load();

What is wrong with this FX Application?

I am hours trying to know why this code is wrong, it gives me an Exception, the GUI.fxml is on the root of the project.
public class MyApp extends Application{
#Override
public void start(Stage primaryStage) throws Exception {
String location = "/GUI.fxml";
Parent root = FXMLLoader.load(getClass().getResource(location));
primaryStage.setScene(new Scene(root,300,450));
primaryStage.setTitle("Minha Janela");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Already searched a lot, no solution found yet.
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:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2825)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
at g1.MyApp.start(MyApp.java:13)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
... 1 more
The FXML should not be in the root of the project, but in the classpath. Try relocating the fxml into a source folder.
For general projects, you have a src folder. You can also create your own custom source folder also.
For maven projects, you can try keeping them inside src/main/resources.

Error loading fxml files from a folder other than the bin folder

I am a fairly new java programmer. I only have about five weeks of experience, starting from zero, and I am having problems getting javafx fxml files created in Scene Builder to load correctly if they are not in the same folder as the controller classes.
I am using
Win7x64 running jre7x86 for this build
Eclipse Juno Service Release 1
Build id: 20120920-0800
jre version 1.7.0_07
javaFx version 2.2.1-b03
SceneBuilder version 1.0-b50
My scene loader code is
private static final String RESOURCE_PATH = "/resources/";
public static Stage buildStage(#SuppressWarnings("rawtypes") Class pClass,
String stageTitle, String resourceLocation)
{
Stage temp = new Stage();
Pane page = null;
try
{
page = FXMLLoader.load(pClass.getResource(RESOURCE_PATH + resourceLocation), null,
new JavaFXBuilderFactory());
}
catch (IOException e)
{
e.printStackTrace();
}
Scene scene = new Scene(page);
temp.setScene(scene);
temp.setTitle(stageTitle);
temp.setResizable(false);
return temp;
}
I know that the project directory is the /workspace/projectName/ so theoretically the loader should be able to find the files if it's given a relative path right? The error I'm getting is
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:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2737)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
at outofunit.system.StageFactory.buildStage(StageFactory.java:32)
at outofunit.desktop.ArcMaster.start(ArcMaster.java:28)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
... 1 more
I've tried giving the loader an absolute path as well but it just isn't accepting anything that isn't directly in the same directory. I'm grasping at straws here trying to figure this out.
I've tried to research this on my own but I haven't gotten much, the few answers on here that seemed similar didn't seem to help, that or I am too dense and/or inexperienced to make sense of them. They are JavaFX 2.0 loading fxml files with event handlers fails and JavaFX 2.0 FXML resource loading error
A buddy of mine was able to overcome this problem by using this code
public void load(String pFileName, Stage pStage, String pTitle)
{
String fName = RESOURCE_PATH + pFileName;
try
{
String externalForm = getClass().getResource(fName)
.toExternalForm();
InputStream inStream = new URL(externalForm).openStream();
FXMLLoader loader = new FXMLLoader();
Pane p = (Pane)loader.load(inStream);
pStage.setTitle(pTitle);
pStage.setScene(new Scene(p));
mWindowControl = loader.getController();
mWindowControl.setUp(pStage);
pStage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
The biggest difference and the reason why I haven't been using his code is because my root pane is an Anchor pane instead of a normal pane, and his way URL instream forces a normal pane. Am I at fault for not using a normal pane as my base? I would love any help or input you guys can give. Thank you.
Edit: So I've been working on this problem and the error message has changed.
I changed the code to this
private final String RESOURCE_PATH = "resources/";
public void load(String pFileName, Stage pStage, String pTitle)
{
String fName = RESOURCE_PATH + pFileName;
Pane page = null;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fName));
try
{
page = (Pane) fxmlLoader.load();
}
catch (IOException exception)
{
throw new RuntimeException(exception);
}
Scene scene = new Scene(page);
pStage.setScene(scene);
pStage.setTitle(pTitle);
mWindowControl = fxmlLoader.getController();
mWindowControl.setUp(pStage);
pStage.show();
}
but the error I'm now getting is
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2021)
at outofunit.desktop.WindowLoader.load(WindowLoader.java:136)
at outofunit.desktop.WindowLoader.load(WindowLoader.java:62)
at outofunit.desktop.ArcMaster.start(ArcMaster.java:30)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Unknown Source)
I don't understand how the location is not being set
It's a path issue, java needs to know where to get the file from the package hierarchy.
So using "/package1/resourcename.fxml" should find the source from the root of the tree.
You normally leave off the "/" at the start as you are already at the top of the package tree.
Both exceptions say "the (FXML) file you are trying to load cannot be found in the location you have provided". Your filename is wrong or its path. Give your package structure or at least the path of class file that includes load(String pFileName, Stage pStage, String pTitle) method and the path of FXML file you want to load. Read the javadoc API of getResource() and examine sample codes about it on the net.

Resources