Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Small JavaFX framework for swapping in and out child panes in a main FXML container. Code is for Java 8+.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?scenebuilder-stylesheet vista.css?>
<VBox prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MainController">
<children>
<Label fx:id="headerLabel" maxWidth="1.7976931348623157E308" text="Header" VBox.vgrow="NEVER" />
<StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
</children>
</VBox>
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;
/**
* Main application class.
*/
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
stage.setTitle("Vista Viewer");
stage.setScene(
createScene(
loadMainPane()
)
);
stage.show();
}
/**
* Loads the main fxml layout.
* Sets up the vista switching VistaNavigator.
* Loads the first vista into the fxml layout.
*
* @return the loaded pane.
* @throws IOException if the pane could not be loaded.
*/
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;
}
/**
* Creates the main application scene.
*
* @param mainPane the main application layout.
*
* @return the created scene.
*/
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(
mainPane
);
scene.getStylesheets().setAll(
getClass().getResource("vista.css").toExternalForm()
);
return scene;
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
/**
* Main controller class for the entire layout.
*/
public class MainController {
/** Holder of a switchable vista. */
@FXML
private StackPane vistaHolder;
/**
* Replaces the vista displayed in the vista holder with a new vista.
*
* @param node the vista node to be swapped in.
*/
public void setVista(Node node) {
vistaHolder.getChildren().setAll(node);
}
}
/**
* vista.css
* Place in the same source directory as Main.java
* Ensure that your build system copies this file to your build output directory.
*/
#headerLabel {
-fx-background-color: steelblue;
-fx-text-fill: white;
-fx-padding: 5px;
}
#vistaHolder {
-fx-background-color: lightgrey;
}
#vista1 {
-fx-background-color: aliceblue;
}
#vista2 {
-fx-background-color: coral;
}
.button {
-fx-base: lightblue;
-fx-font-size: 20px;
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?scenebuilder-stylesheet vista.css?>
<StackPane fx:id="vista1" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista1Controller">
<children>
<Button mnemonicParsing="false" onAction="#nextPane" text="Next Pane" />
</children>
</StackPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
/**
* Controller class for the first vista.
*/
public class Vista1Controller {
/**
* Event handler fired when the user requests a new vista.
*
* @param event the event that triggered the handler.
*/
@FXML
void nextPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?scenebuilder-stylesheet vista.css?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="vista2" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista2Controller">
<children>
<Button mnemonicParsing="false" onAction="#previousPane" text="Previous Pane" />
</children>
</StackPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
/**
* Controller class for the second vista.
*/
public class Vista2Controller {
/**
* Event handler fired when the user requests a previous vista.
*
* @param event the event that triggered the handler.
*/
@FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
}
import javafx.fxml.FXMLLoader;
import java.io.IOException;
/**
* Utility class for controlling navigation between vistas.
*
* All methods on the navigator are static to facilitate
* simple access from anywhere in the application.
*/
public class VistaNavigator {
/**
* Convenience constants for fxml layouts managed by the navigator.
*/
public static final String MAIN = "main.fxml";
public static final String VISTA_1 = "vista1.fxml";
public static final String VISTA_2 = "vista2.fxml";
/** The main application layout controller. */
private static MainController mainController;
/**
* Stores the main controller for later use in navigation tasks.
*
* @param mainController the main application layout controller.
*/
public static void setMainController(MainController mainController) {
VistaNavigator.mainController = mainController;
}
/**
* Loads the vista specified by the fxml file into the
* vistaHolder pane of the main application layout.
*
* Previously loaded vista for the same fxml file are not cached.
* The fxml is loaded anew and a new vista node hierarchy generated
* every time this method is invoked.
*
* A more sophisticated load function could potentially add some
* enhancements or optimizations, for example:
* cache FXMLLoaders
* cache loaded vista nodes, so they can be recalled or reused
* allow a user to specify vista node reuse or new creation
* allow back and forward history like a browser
*
* @param fxml the fxml file to be loaded.
*/
public static void loadVista(String fxml) {
try {
mainController.setVista(
FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@jewelsea

This comment has been minimized.

Copy link
Owner Author

@jewelsea jewelsea commented Sep 6, 2013

Answer to StackOverflow Question: Loading new fxml in the same scene.

@bigesta

This comment has been minimized.

Copy link

@bigesta bigesta commented Sep 15, 2013

Hi jewelsea. Can u please make it into a netbeans project, i tried it and i get a lot errors.

@bigesta

This comment has been minimized.

Copy link

@bigesta bigesta commented Sep 16, 2013

I tried to create a netbeans project with the above code and i got a lot of errors....

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.javafx.main.Main.launchApp(Main.java:642)
at com.javafx.main.Main.main(Main.java:805)
Caused by: 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(Thread.java:722)
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2056)
at main.Main.loadMainPane(Main.java:44)
at main.Main.start(Main.java:24)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
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
Java Result: 1

i changed this portion in VistaNavigator.java for it to compile

public static void loadVista(String fxml) {
    try {
        mainController.setVista(
            (Node) FXMLLoader.load(           /***<----***/
                VistaNavigator.class.getResource(
                    fxml
                )
            ));
    } catch (IOException e) {
        //e.printStackTrace();          /***<----***/
    }
}

here is the link to the netbeans project

@jewelsea

This comment has been minimized.

Copy link
Owner Author

@jewelsea jewelsea commented Sep 16, 2013

@bigesta

The code in this post works fine in Java 8 if all of the code is placed in the required directory, which is the top level source directory, not a package directory.

I created a netbeans project for you which works for me against Java7u40 on OS X 10.8 using NetBeans 7.3.1. The netbeans project is temporarily hosted off my website, so that you may download it.

The reason why you need to add the (Node) cast to the loadVista function is that the framework was written for Java 8 and you are using an earlier Java version. Java 7 is not as clever (or less lenient) about type inference, so Java 7 requires an explicit cast to allow compilation, whilst Java 8 does not.

The reason your FXML load call failed (the NullPointerException) is likely because the framework code assumes that everything is not placed in a named Java package. Perhaps you placed everything in a package named after your project (which is the default way that NetBeans sets up new projects, though my IDE Idea does not work that way). To allow the code to work under a different package, you can modify the fx:controller statement in main.fxml to include your package name. For the sample netbeans project I linked off of this comment, the code is not placed in a package, so it does not replicate your issue.

@bigesta

This comment has been minimized.

Copy link

@bigesta bigesta commented Sep 16, 2013

Wow...it works. thank you very much for your help :). I have a long way to go... but hey 1 step at a time right. Again thank you

@psybe

This comment has been minimized.

Copy link

@psybe psybe commented Oct 4, 2013

Great work jewelsea! Nice every day help on stackoverflow!

@allan-farrell

This comment has been minimized.

Copy link

@allan-farrell allan-farrell commented Oct 12, 2013

Oracle should hire you Jewelsea! This kind of helpful stuff is sorely lacking in their tutorials.

@jellyjordan

This comment has been minimized.

Copy link

@jellyjordan jellyjordan commented Apr 9, 2014

Thank you! This has been the most helpful resource for helping me develop menu navigation.

@cuongict

This comment has been minimized.

Copy link

@cuongict cuongict commented May 16, 2014

Thank you very much for your solution. I has been in troubles with this problem and your solution helps me so much

@eemarwell

This comment has been minimized.

Copy link

@eemarwell eemarwell commented Oct 10, 2014

Hello! I'm trying to use your framework in Netbeans using JDK 8, but when I try to change the packages (.fxml in views package and Controllers in crontrollers package) I get some errors. How can I do it the right way?

@eemarwell

This comment has been minimized.

Copy link

@eemarwell eemarwell commented Oct 10, 2014

Look... I have this working: http://i.imgur.com/fdHjAw9.png
But I want something like this: http://i.imgur.com/IdZWiC3.png
Can you help me?

@kamenitxan

This comment has been minimized.

Copy link

@kamenitxan kamenitxan commented Dec 13, 2014

I modified this and created .jar framework. Usage is simpler a it have some new features.
https://github.com/kamenitxan/SceneSwitcher

@nazmuddin

This comment has been minimized.

Copy link

@nazmuddin nazmuddin commented Jan 13, 2015

Hello! how to use this if i want to place my view ( .fxml files )and controllers in to different packages.
I tried to change the code in VistaNavigator class like this

public static final String MAIN    = "/view/main.fxml";
public static final String VISTA_1 = "/view/vista1.fxml";
public static final String VISTA_2 = "/view/vista2.fxml";

But it still showing nullPointerException.

@cjanjeric

This comment has been minimized.

Copy link

@cjanjeric cjanjeric commented Jan 13, 2015

Hi nazmuddin just like what jewelsea said earlier you should also change the fx:controller statement in main.fxml, vista1.fxml and the vista2.fxml to your desired package name, happy codings

@nazmuddin

This comment has been minimized.

Copy link

@nazmuddin nazmuddin commented Jan 13, 2015

@ cjanjeric, Thanks for this quick reply i did it already, but it is showing nullpoiinter exception. I read somewhere on the stackoverflow that, fxml must be in the same package of it's loader class.
I also gone through this thread for how to communicate between the controller for the "outer" FXML file and the controller for the included fxml file and make some changes but it didn't helped me too.
Hope some help help from you guys.

@nazmuddin

This comment has been minimized.

Copy link

@nazmuddin nazmuddin commented Jan 14, 2015

Thank you very much jewelsea for something i was looking for desperately.

@eemarwell - move these fxml files in view package and change the code in VistaNavigator class like this

public static final String MAIN = "/view/main.fxml";
public static final String VISTA_1 = "/view/vista1.fxml";
public static final String VISTA_2 = "/view/vista2.fxml";

now save it and if you are using netbeans (i think so) then right click on project and click clean and build and then run. It worked for me like that.

@jewelsea

This comment has been minimized.

Copy link
Owner Author

@jewelsea jewelsea commented Jan 26, 2015

I do not receive notification of comments on this gist. The best place to ask questions about this gist (or other JavaFX topics), is usually StackOverflow.

@miichu

This comment has been minimized.

Copy link

@miichu miichu commented Feb 27, 2015

I finally managed to switch screens! Thank you so much for this code!! sending virtual flowers and chocolates

@flashery

This comment has been minimized.

Copy link

@flashery flashery commented Mar 15, 2015

Hello having an error in Main.java

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:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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.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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/28230490.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
unknown path:7

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:918)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:967)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2419)
at application.Main.loadMainPane(Main.java:40)
at application.Main.start(Main.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/3467110.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/11567059.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/31218869.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/30452001.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/2075313.run(Unknown Source)
... 1 more

Caused by: java.lang.ClassNotFoundException: MainController
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:916)
... 22 more
Exception running application application.Main

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Mar 23, 2015

Great Piece of code ! thx for sharing

@ahn94

This comment has been minimized.

Copy link

@ahn94 ahn94 commented Aug 5, 2015

Very helpful!! Thanks for sharing!!!

@albort

This comment has been minimized.

Copy link

@albort albort commented Oct 10, 2015

hello, this piece of code is great!, I was wondering if vista1 and vista2, could be made to fit the pane from main.fmxl.. I would really appreciate your help..
Thanks for sharing!

@nicolasparada

This comment has been minimized.

Copy link

@nicolasparada nicolasparada commented Nov 17, 2015

Helped a lot :D

@jewelsea

This comment has been minimized.

Copy link
Owner Author

@jewelsea jewelsea commented Apr 7, 2016

A simplified navigation system which does not use fxml, use a centralized navigation controller, does not cache scenes or controllers and completely replaces the scene content on each navigation can be found in the answer to Changing Scenes in JavaFX

@teymaronix

This comment has been minimized.

Copy link

@teymaronix teymaronix commented Apr 22, 2016

Hello @jewelsea
I saw your your answer on Loading new fxml in the same scene.
The Problem is i couldn't add a comment there because i don't have the needed 50 reputations and i can't comment...

I tryed this method
content.getChildren().setAll(FXMLLoader.load("vista2.fxml"));
but i always get an error. In my case i'm using this code:
`package ezmovie;

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.control.Button;
import javafx.scene.layout.AnchorPane;

public class controller_home implements Initializable {

@FXML
protected Button btnSuchen, btnHome, btnFavoriten, btnListen, btnSerien, btnFilme, btnLogout, btnSettings;
@FXML
private AnchorPane aContent;


@FXML
protected void test(ActionEvent event) {
    aContent.getChildren().setAll(FXMLLoader.load("view/ezMovieContent.fxml"));
}

@Override
public void initialize(URL url, ResourceBundle rb) {

}

}
`
I get this error message:

The method load(InputStream) in the type FXMLLoader is not applicable for the arguments (String)

This Error appears on .load().

Sorry that i contact you over here about this problem, but i could not find any mail adress or other option to contact you.
Thank you and kind regards,
Timo

@Jeffrey-Kimani

This comment has been minimized.

Copy link

@Jeffrey-Kimani Jeffrey-Kimani commented Oct 11, 2016

I am trying to load the fxml using
private AnchorPane anpNavigate;
anpNavigate.getChildren().setAll(FXMLLoader.load("Simple.fxml"));

but it's bringing an error
load cannot be applied to a String

@ivan047

This comment has been minimized.

Copy link

@ivan047 ivan047 commented Mar 4, 2017

@bigesta and those who have issues launching this code, you need to add the package name before the names of you FX controllers in .fxml files, so that:
fx:controller="sampleNameOfYourPackage.MainController">

Same to be done with vista1.fxml and vista2.fxml

@iamazik

This comment has been minimized.

Copy link

@iamazik iamazik commented Mar 14, 2017

This worked like a charm for me. I was playing with this on a Maven directory structure project and had to change the path of the fxml and css files. This is one of the clean navigator.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 10, 2017

Hello @jewelsea
I tried your code on StackOverflow but getting the error on load
------ content.getChildren().setAll(FXMLLoader.load("main.fxml"));

Please help. I am stuck at it whole day. Tried your above code also but instead of StackPane I used AnchorPane it is working but the controller is getting set on the header label when I press the next button where as in your case the header label is visible when you press next Button. I am designing an applcation which has 2 menubars on top as well as on side. Please help. Thank You in Advance

@BoGnY

This comment has been minimized.

Copy link

@BoGnY BoGnY commented Jan 19, 2018

I have built an example project with difference that there are packages and Gradle build system..
Then I have built also an example project with packages and Ant as default build system, and an example project with packages and Maven!!

All examples are based on your code 😃

@vigneshrcsengg

This comment has been minimized.

Copy link

@vigneshrcsengg vigneshrcsengg commented Apr 19, 2018

@jwelsea
Great framework, switching fxml page by using panes. I make it on work. Unfortunately i encountred an error with loading image, if i use your logic to load panes.

Image, stylesheet paths are not loaded as excepted like ../ui/header.jpg

The result in log i see 'null'

null/../ui/header.jpg

jfx-project-run:
Executing D:\DN-Workspace\DBS\dbs-dnstaging\dist\run1664401303\dbs-dnstaging.jar using platform C:\Program Files\Java\jdk1.8.0_162\jre/bin/java
null/../ui/header.jpg
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: javafx.fxml.LoadException: 
unknown path:24

	at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
	at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
	at com.dn.staging.main.DNStagingApplStart.loadMainPane(DNStagingApplStart.java:61)
	at com.dn.staging.main.DNStagingApplStart.start(DNStagingApplStart.java:45)
	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
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
	at javafx.scene.image.Image.validateUrl(Image.java:1118)
	at javafx.scene.image.Image.<init>(Image.java:693)
	at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
	at com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
	at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:763)
	at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
	at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
	... 12 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
	at javafx.scene.image.Image.validateUrl(Image.java:1110)
	... 18 more
Exception running application com.dn.staging.main.DNStagingApplStart
Java Result: 1

If I load fxml files as usual way by parent class like below, my application can get load the images.

Parent root = FXMLLoader.load(getClass().getResource("/com/dn/staging/config/terminal/TerminalConfiguration.fxml"));
primaryStage.setTitle(appTitle());
primaryStage.setScene(new Scene(root));
//primaryStage.setFullScreen(true);
primaryStage.show();

What should be the probelm?? I'm new FX technology.

@KANE-99

This comment has been minimized.

Copy link

@KANE-99 KANE-99 commented Sep 17, 2018

Thanks Man!!! You are Awesome 👍 :)

@cagatayyigit

This comment has been minimized.

Copy link

@cagatayyigit cagatayyigit commented May 9, 2019

thanks for sharing. that is great

@MrBahur

This comment has been minimized.

Copy link

@MrBahur MrBahur commented May 17, 2020

thanks for sharing! awseome work.
I can send you a Maven project with packages and resources folder if you want.
(just needed to change a bit)

@engrmafzaalch

This comment has been minimized.

Copy link

@engrmafzaalch engrmafzaalch commented Aug 14, 2020

Hey Thank you for this.... But i am facing an issue where i need to pass argument while loading a new fxml file .....i need to pass an argument while loading new fxml file kindly help me there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment