1

I am making a simple Javafx application with FXML. My files are all in the same package:

  • MainApp.java
  • MainAppController.java
  • style.css
  • MainApp.fxml

My root tag from the .fxml file contains:

<GridPane fx:id="myRootPanel"
          alignment="CENTER"
          maxHeight="-Infinity" maxWidth="-Infinity"
          minHeight="-Infinity" minWidth="-Infinity" 
          stylesheets="@style.css"
          xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="sample.MainAppController">

as you see there is a stylesheets="@style.css", which was even autocompleted by Intellij.

When I open this .fxml file in Scenebuilder, the css is applied correctly, but when I run the project, I get following console messages:

null/style.css and WARNING: Resource "style.css" not found.

I have added *.css to my Resource patterns, did a clean build, invalidated cache, but none of these resolve my problem.

I tried to make a simple application in NetBeans, and when I run that project there, the CSS is applied correctly. Because of this, I think there might be a problem/setting in the Intellij IDE to solve this... Does anyone know what to do?

Thanks in advance.
B.

fabian
  • 80,457
  • 12
  • 86
  • 114
BenjaVR
  • 530
  • 5
  • 15

2 Answers2

0

Seems like the @style.css does not work, instead I used the relative folder path, starting from the root (src): /css/style.css.

BenjaVR
  • 530
  • 5
  • 15
0

I had to do it a bit differently. The problem was that I create the CSS dynamically based on stored settings (e.g. font family, font size, etc. with selection by the user). I called it dynamic.css.

So, what worked for me in my Main Window Controller was this:

String csspath = "";
try
{
    csspath = MainController.class.getResource("/CSS/dynamic.css").toExternalForm();
    mainscene.getStylesheets().add(csspath);
}
catch ( Exception e )
{
    String msg = "\nERROR MainController::loadStylesheet 2040: err=";
    msg += e.getMessage();
    msg += "\nURL:";
    msg += csspath;
    msg += "\n\n";
    System.out.println(msg);
    mainscene.setCursor(Cursor.DEFAULT);
}
moken
  • 3,227
  • 8
  • 13
  • 23
AD5XJ
  • 31
  • 3