Connected styles in Scene Builder is not found in runtime












0















I create Pane in Scene Builder for Java 8. My .css files store in /rescouces/css/app.css. I connect stylesheet in Scene Builder and all ok. But after i start my app i get exception with error:
Caused by: javafx.fxml.LoadException: Invalid resource: /../style/app.css not found on the classpath.



How to fix this? I need every time rename path to css in .fxml?



enter image description here



<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="912.0" prefWidth="1368.0" styleClass="app" stylesheets="@/style/app.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mypod.tablet.controller.MainController">
<children>
<AnchorPane fx:id="contentPane" layoutX="248.0" layoutY="138.0" stylesheets="@/style/content.css" AnchorPane.bottomAnchor="94.0" AnchorPane.leftAnchor="250.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="115.0">
<styleClass>
<String fx:value="block" />
<String fx:value="content-block" />
</styleClass>
</AnchorPane>
</children>
</AnchorPane>


Load fxml:



FXMLLoader loader = new FXMLLoader();

this.primaryStage.setScene(new Scene(loader.load(Util.getResource("/fxml/main.fxml"))));









share|improve this question

























  • Can you edit your question and include somehow the structure of your project? At least the main folders (including resources), and also where and how do you try to load the app.css file?

    – José Pereda
    Nov 20 '18 at 10:44











  • @JoséPereda i updated my question. I connect css file in Scene Builder in stylesheets block. This css connection exist in fxml file.

    – Tsyklop
    Nov 20 '18 at 20:54











  • Ok, thanks, maybe you can post that bit of the FXML file as well, that would help.

    – José Pereda
    Nov 20 '18 at 20:56











  • @JoséPereda FXML wherein attached css?

    – Tsyklop
    Nov 20 '18 at 21:19











  • Just edit the FXML file and copy/paste the code relevant to the css part.

    – José Pereda
    Nov 20 '18 at 21:20
















0















I create Pane in Scene Builder for Java 8. My .css files store in /rescouces/css/app.css. I connect stylesheet in Scene Builder and all ok. But after i start my app i get exception with error:
Caused by: javafx.fxml.LoadException: Invalid resource: /../style/app.css not found on the classpath.



How to fix this? I need every time rename path to css in .fxml?



enter image description here



<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="912.0" prefWidth="1368.0" styleClass="app" stylesheets="@/style/app.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mypod.tablet.controller.MainController">
<children>
<AnchorPane fx:id="contentPane" layoutX="248.0" layoutY="138.0" stylesheets="@/style/content.css" AnchorPane.bottomAnchor="94.0" AnchorPane.leftAnchor="250.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="115.0">
<styleClass>
<String fx:value="block" />
<String fx:value="content-block" />
</styleClass>
</AnchorPane>
</children>
</AnchorPane>


Load fxml:



FXMLLoader loader = new FXMLLoader();

this.primaryStage.setScene(new Scene(loader.load(Util.getResource("/fxml/main.fxml"))));









share|improve this question

























  • Can you edit your question and include somehow the structure of your project? At least the main folders (including resources), and also where and how do you try to load the app.css file?

    – José Pereda
    Nov 20 '18 at 10:44











  • @JoséPereda i updated my question. I connect css file in Scene Builder in stylesheets block. This css connection exist in fxml file.

    – Tsyklop
    Nov 20 '18 at 20:54











  • Ok, thanks, maybe you can post that bit of the FXML file as well, that would help.

    – José Pereda
    Nov 20 '18 at 20:56











  • @JoséPereda FXML wherein attached css?

    – Tsyklop
    Nov 20 '18 at 21:19











  • Just edit the FXML file and copy/paste the code relevant to the css part.

    – José Pereda
    Nov 20 '18 at 21:20














0












0








0








I create Pane in Scene Builder for Java 8. My .css files store in /rescouces/css/app.css. I connect stylesheet in Scene Builder and all ok. But after i start my app i get exception with error:
Caused by: javafx.fxml.LoadException: Invalid resource: /../style/app.css not found on the classpath.



How to fix this? I need every time rename path to css in .fxml?



enter image description here



<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="912.0" prefWidth="1368.0" styleClass="app" stylesheets="@/style/app.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mypod.tablet.controller.MainController">
<children>
<AnchorPane fx:id="contentPane" layoutX="248.0" layoutY="138.0" stylesheets="@/style/content.css" AnchorPane.bottomAnchor="94.0" AnchorPane.leftAnchor="250.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="115.0">
<styleClass>
<String fx:value="block" />
<String fx:value="content-block" />
</styleClass>
</AnchorPane>
</children>
</AnchorPane>


Load fxml:



FXMLLoader loader = new FXMLLoader();

this.primaryStage.setScene(new Scene(loader.load(Util.getResource("/fxml/main.fxml"))));









share|improve this question
















I create Pane in Scene Builder for Java 8. My .css files store in /rescouces/css/app.css. I connect stylesheet in Scene Builder and all ok. But after i start my app i get exception with error:
Caused by: javafx.fxml.LoadException: Invalid resource: /../style/app.css not found on the classpath.



How to fix this? I need every time rename path to css in .fxml?



enter image description here



<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="912.0" prefWidth="1368.0" styleClass="app" stylesheets="@/style/app.css" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mypod.tablet.controller.MainController">
<children>
<AnchorPane fx:id="contentPane" layoutX="248.0" layoutY="138.0" stylesheets="@/style/content.css" AnchorPane.bottomAnchor="94.0" AnchorPane.leftAnchor="250.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="115.0">
<styleClass>
<String fx:value="block" />
<String fx:value="content-block" />
</styleClass>
</AnchorPane>
</children>
</AnchorPane>


Load fxml:



FXMLLoader loader = new FXMLLoader();

this.primaryStage.setScene(new Scene(loader.load(Util.getResource("/fxml/main.fxml"))));






styles scenebuilder gluon






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 17:26







Tsyklop

















asked Nov 14 '18 at 21:58









TsyklopTsyklop

87113




87113













  • Can you edit your question and include somehow the structure of your project? At least the main folders (including resources), and also where and how do you try to load the app.css file?

    – José Pereda
    Nov 20 '18 at 10:44











  • @JoséPereda i updated my question. I connect css file in Scene Builder in stylesheets block. This css connection exist in fxml file.

    – Tsyklop
    Nov 20 '18 at 20:54











  • Ok, thanks, maybe you can post that bit of the FXML file as well, that would help.

    – José Pereda
    Nov 20 '18 at 20:56











  • @JoséPereda FXML wherein attached css?

    – Tsyklop
    Nov 20 '18 at 21:19











  • Just edit the FXML file and copy/paste the code relevant to the css part.

    – José Pereda
    Nov 20 '18 at 21:20



















  • Can you edit your question and include somehow the structure of your project? At least the main folders (including resources), and also where and how do you try to load the app.css file?

    – José Pereda
    Nov 20 '18 at 10:44











  • @JoséPereda i updated my question. I connect css file in Scene Builder in stylesheets block. This css connection exist in fxml file.

    – Tsyklop
    Nov 20 '18 at 20:54











  • Ok, thanks, maybe you can post that bit of the FXML file as well, that would help.

    – José Pereda
    Nov 20 '18 at 20:56











  • @JoséPereda FXML wherein attached css?

    – Tsyklop
    Nov 20 '18 at 21:19











  • Just edit the FXML file and copy/paste the code relevant to the css part.

    – José Pereda
    Nov 20 '18 at 21:20

















Can you edit your question and include somehow the structure of your project? At least the main folders (including resources), and also where and how do you try to load the app.css file?

– José Pereda
Nov 20 '18 at 10:44





Can you edit your question and include somehow the structure of your project? At least the main folders (including resources), and also where and how do you try to load the app.css file?

– José Pereda
Nov 20 '18 at 10:44













@JoséPereda i updated my question. I connect css file in Scene Builder in stylesheets block. This css connection exist in fxml file.

– Tsyklop
Nov 20 '18 at 20:54





@JoséPereda i updated my question. I connect css file in Scene Builder in stylesheets block. This css connection exist in fxml file.

– Tsyklop
Nov 20 '18 at 20:54













Ok, thanks, maybe you can post that bit of the FXML file as well, that would help.

– José Pereda
Nov 20 '18 at 20:56





Ok, thanks, maybe you can post that bit of the FXML file as well, that would help.

– José Pereda
Nov 20 '18 at 20:56













@JoséPereda FXML wherein attached css?

– Tsyklop
Nov 20 '18 at 21:19





@JoséPereda FXML wherein attached css?

– Tsyklop
Nov 20 '18 at 21:19













Just edit the FXML file and copy/paste the code relevant to the css part.

– José Pereda
Nov 20 '18 at 21:20





Just edit the FXML file and copy/paste the code relevant to the css part.

– José Pereda
Nov 20 '18 at 21:20












1 Answer
1






active

oldest

votes


















1





+50









The problem



In order to reproduce the issue, and based on the comments for the question, this is required:



Main class



@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResourceAsStream("fxml/scene.fxml"));

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}


FXML under src/main/resources/fxml/scene.fxml:



<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" stylesheets="@../styles/styles.css" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="button" text="Click Me!" />
</children>
</AnchorPane>


CSS under src/main/resources/styles/styles.css



.button {
-fx-font-size: 2em;
}


The project runs, but you get this error printed:



null/../styles/styles.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "../styles/styles.css" not found.


While manually editing the FXML file and removing the parent dots:



stylesheets="@/styles/styles.css"


seems to solve the issue and runs fine without warning, this prevents Scene Builder from finding the css file, so it shouldn't be done.



The solution




  • Using getResourceAsStream to retrieve the FXML file is not recommended, just use getResource().


This works:



FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Using the FXMLLoader empty constructor is not the recommended way, instead use the the static load method.


This works:



Parent root = FXMLLoader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Finally, there is no need for class loader. The classloader is stream based, and it doesn't know about the class you retrieve it from and tries to locate from the package "fxml/scene.fxml". On the other hand, Class.getResource() is URL based and it looks up the resource relative to the class, so you need to set the path to the root of the project "/fxml/scene.fxml".


This is how it should be called:



Parent root = FXMLLoader.load(MainApp.class.getResource("/fxml/scene.fxml"));


Or in case you need the loader (for instance to retrieve the controller), this is also the recommended way:



FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/scene.fxml"));
// YourController controller = (YourController) loader.getController();
Parent root = loader.load();


This post is worth reading.






share|improve this answer
























  • Thank you so much!!!

    – Tsyklop
    Nov 23 '18 at 20:09











  • How it works in jar file?

    – Tsyklop
    Nov 23 '18 at 21:30











  • What do you mean? It does work from a jar actually.

    – José Pereda
    Nov 23 '18 at 21:41











  • Did you check this? Because I had problems with getResource() method in jar file?

    – Tsyklop
    Nov 23 '18 at 21:58













  • Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

    – José Pereda
    Nov 23 '18 at 22:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309349%2fconnected-styles-in-scene-builder-is-not-found-in-runtime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1





+50









The problem



In order to reproduce the issue, and based on the comments for the question, this is required:



Main class



@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResourceAsStream("fxml/scene.fxml"));

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}


FXML under src/main/resources/fxml/scene.fxml:



<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" stylesheets="@../styles/styles.css" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="button" text="Click Me!" />
</children>
</AnchorPane>


CSS under src/main/resources/styles/styles.css



.button {
-fx-font-size: 2em;
}


The project runs, but you get this error printed:



null/../styles/styles.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "../styles/styles.css" not found.


While manually editing the FXML file and removing the parent dots:



stylesheets="@/styles/styles.css"


seems to solve the issue and runs fine without warning, this prevents Scene Builder from finding the css file, so it shouldn't be done.



The solution




  • Using getResourceAsStream to retrieve the FXML file is not recommended, just use getResource().


This works:



FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Using the FXMLLoader empty constructor is not the recommended way, instead use the the static load method.


This works:



Parent root = FXMLLoader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Finally, there is no need for class loader. The classloader is stream based, and it doesn't know about the class you retrieve it from and tries to locate from the package "fxml/scene.fxml". On the other hand, Class.getResource() is URL based and it looks up the resource relative to the class, so you need to set the path to the root of the project "/fxml/scene.fxml".


This is how it should be called:



Parent root = FXMLLoader.load(MainApp.class.getResource("/fxml/scene.fxml"));


Or in case you need the loader (for instance to retrieve the controller), this is also the recommended way:



FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/scene.fxml"));
// YourController controller = (YourController) loader.getController();
Parent root = loader.load();


This post is worth reading.






share|improve this answer
























  • Thank you so much!!!

    – Tsyklop
    Nov 23 '18 at 20:09











  • How it works in jar file?

    – Tsyklop
    Nov 23 '18 at 21:30











  • What do you mean? It does work from a jar actually.

    – José Pereda
    Nov 23 '18 at 21:41











  • Did you check this? Because I had problems with getResource() method in jar file?

    – Tsyklop
    Nov 23 '18 at 21:58













  • Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

    – José Pereda
    Nov 23 '18 at 22:19
















1





+50









The problem



In order to reproduce the issue, and based on the comments for the question, this is required:



Main class



@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResourceAsStream("fxml/scene.fxml"));

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}


FXML under src/main/resources/fxml/scene.fxml:



<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" stylesheets="@../styles/styles.css" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="button" text="Click Me!" />
</children>
</AnchorPane>


CSS under src/main/resources/styles/styles.css



.button {
-fx-font-size: 2em;
}


The project runs, but you get this error printed:



null/../styles/styles.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "../styles/styles.css" not found.


While manually editing the FXML file and removing the parent dots:



stylesheets="@/styles/styles.css"


seems to solve the issue and runs fine without warning, this prevents Scene Builder from finding the css file, so it shouldn't be done.



The solution




  • Using getResourceAsStream to retrieve the FXML file is not recommended, just use getResource().


This works:



FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Using the FXMLLoader empty constructor is not the recommended way, instead use the the static load method.


This works:



Parent root = FXMLLoader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Finally, there is no need for class loader. The classloader is stream based, and it doesn't know about the class you retrieve it from and tries to locate from the package "fxml/scene.fxml". On the other hand, Class.getResource() is URL based and it looks up the resource relative to the class, so you need to set the path to the root of the project "/fxml/scene.fxml".


This is how it should be called:



Parent root = FXMLLoader.load(MainApp.class.getResource("/fxml/scene.fxml"));


Or in case you need the loader (for instance to retrieve the controller), this is also the recommended way:



FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/scene.fxml"));
// YourController controller = (YourController) loader.getController();
Parent root = loader.load();


This post is worth reading.






share|improve this answer
























  • Thank you so much!!!

    – Tsyklop
    Nov 23 '18 at 20:09











  • How it works in jar file?

    – Tsyklop
    Nov 23 '18 at 21:30











  • What do you mean? It does work from a jar actually.

    – José Pereda
    Nov 23 '18 at 21:41











  • Did you check this? Because I had problems with getResource() method in jar file?

    – Tsyklop
    Nov 23 '18 at 21:58













  • Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

    – José Pereda
    Nov 23 '18 at 22:19














1





+50







1





+50



1




+50





The problem



In order to reproduce the issue, and based on the comments for the question, this is required:



Main class



@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResourceAsStream("fxml/scene.fxml"));

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}


FXML under src/main/resources/fxml/scene.fxml:



<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" stylesheets="@../styles/styles.css" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="button" text="Click Me!" />
</children>
</AnchorPane>


CSS under src/main/resources/styles/styles.css



.button {
-fx-font-size: 2em;
}


The project runs, but you get this error printed:



null/../styles/styles.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "../styles/styles.css" not found.


While manually editing the FXML file and removing the parent dots:



stylesheets="@/styles/styles.css"


seems to solve the issue and runs fine without warning, this prevents Scene Builder from finding the css file, so it shouldn't be done.



The solution




  • Using getResourceAsStream to retrieve the FXML file is not recommended, just use getResource().


This works:



FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Using the FXMLLoader empty constructor is not the recommended way, instead use the the static load method.


This works:



Parent root = FXMLLoader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Finally, there is no need for class loader. The classloader is stream based, and it doesn't know about the class you retrieve it from and tries to locate from the package "fxml/scene.fxml". On the other hand, Class.getResource() is URL based and it looks up the resource relative to the class, so you need to set the path to the root of the project "/fxml/scene.fxml".


This is how it should be called:



Parent root = FXMLLoader.load(MainApp.class.getResource("/fxml/scene.fxml"));


Or in case you need the loader (for instance to retrieve the controller), this is also the recommended way:



FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/scene.fxml"));
// YourController controller = (YourController) loader.getController();
Parent root = loader.load();


This post is worth reading.






share|improve this answer













The problem



In order to reproduce the issue, and based on the comments for the question, this is required:



Main class



@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResourceAsStream("fxml/scene.fxml"));

Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}


FXML under src/main/resources/fxml/scene.fxml:



<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" stylesheets="@../styles/styles.css" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button fx:id="button" text="Click Me!" />
</children>
</AnchorPane>


CSS under src/main/resources/styles/styles.css



.button {
-fx-font-size: 2em;
}


The project runs, but you get this error printed:



null/../styles/styles.css
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "../styles/styles.css" not found.


While manually editing the FXML file and removing the parent dots:



stylesheets="@/styles/styles.css"


seems to solve the issue and runs fine without warning, this prevents Scene Builder from finding the css file, so it shouldn't be done.



The solution




  • Using getResourceAsStream to retrieve the FXML file is not recommended, just use getResource().


This works:



FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Using the FXMLLoader empty constructor is not the recommended way, instead use the the static load method.


This works:



Parent root = FXMLLoader.load(MainApp.class.getClassLoader().getResource("fxml/scene.fxml"));



  • Finally, there is no need for class loader. The classloader is stream based, and it doesn't know about the class you retrieve it from and tries to locate from the package "fxml/scene.fxml". On the other hand, Class.getResource() is URL based and it looks up the resource relative to the class, so you need to set the path to the root of the project "/fxml/scene.fxml".


This is how it should be called:



Parent root = FXMLLoader.load(MainApp.class.getResource("/fxml/scene.fxml"));


Or in case you need the loader (for instance to retrieve the controller), this is also the recommended way:



FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/fxml/scene.fxml"));
// YourController controller = (YourController) loader.getController();
Parent root = loader.load();


This post is worth reading.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 18:24









José PeredaJosé Pereda

26.3k34369




26.3k34369













  • Thank you so much!!!

    – Tsyklop
    Nov 23 '18 at 20:09











  • How it works in jar file?

    – Tsyklop
    Nov 23 '18 at 21:30











  • What do you mean? It does work from a jar actually.

    – José Pereda
    Nov 23 '18 at 21:41











  • Did you check this? Because I had problems with getResource() method in jar file?

    – Tsyklop
    Nov 23 '18 at 21:58













  • Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

    – José Pereda
    Nov 23 '18 at 22:19



















  • Thank you so much!!!

    – Tsyklop
    Nov 23 '18 at 20:09











  • How it works in jar file?

    – Tsyklop
    Nov 23 '18 at 21:30











  • What do you mean? It does work from a jar actually.

    – José Pereda
    Nov 23 '18 at 21:41











  • Did you check this? Because I had problems with getResource() method in jar file?

    – Tsyklop
    Nov 23 '18 at 21:58













  • Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

    – José Pereda
    Nov 23 '18 at 22:19

















Thank you so much!!!

– Tsyklop
Nov 23 '18 at 20:09





Thank you so much!!!

– Tsyklop
Nov 23 '18 at 20:09













How it works in jar file?

– Tsyklop
Nov 23 '18 at 21:30





How it works in jar file?

– Tsyklop
Nov 23 '18 at 21:30













What do you mean? It does work from a jar actually.

– José Pereda
Nov 23 '18 at 21:41





What do you mean? It does work from a jar actually.

– José Pereda
Nov 23 '18 at 21:41













Did you check this? Because I had problems with getResource() method in jar file?

– Tsyklop
Nov 23 '18 at 21:58







Did you check this? Because I had problems with getResource() method in jar file?

– Tsyklop
Nov 23 '18 at 21:58















Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

– José Pereda
Nov 23 '18 at 22:19





Yes, getResource() is the usual way. Of course, you can still use getResourceAsStream(), to load an Image from a file, for instance. Anyway, if you have any issue, post a new question, it is better than trying to solve it over comments.

– José Pereda
Nov 23 '18 at 22:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309349%2fconnected-styles-in-scene-builder-is-not-found-in-runtime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine