0

I tried all the methods written in the stackoverflow, but all these methods did not work for me.

These are the methods I tried:

Everything works well in the scene builder, but when I run the program, this comes out:

nove. 01, 2021 9:10:50 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged   
INFO: Could not find stylesheet:                            
file:/C:/Users/User/Desktop/Coursework/AppLibrary/target/classes/com/example/applibrary/style.css  
nove. 01, 2021 9:10:50 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged  
WARNING: Resource "src/main/resources/styles.css" not found.

My code looks like this:

package com.example.applibrary;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;

public class HelloApplication extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("login.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 1000, 500);
        //
        scene.getStylesheets().add("src/main/resources/styles.css");
        //
        stage.setResizable(false);
        stage.setTitle("Login");
        stage.setScene(scene);
        stage.centerOnScreen();
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

My folders look like this:

  • src
    • main
      • java
      • resources
        • style.css
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • See the [eden coding guide to JavaFX resources](https://edencoding.com/where-to-put-resource-files-in-javafx/) – jewelsea Nov 01 '21 at 15:24
  • 1
    Including `src` In a resource lookup path is always wrong. – jewelsea Nov 01 '21 at 15:26
  • didn't help, after this code mainScene.getStylesheets (). add (App.class.getResource ("style.css"). toExternalForm ()); the compiler threw out a new error and now my program won't start at all – it_would_be_better Nov 01 '21 at 15:27
  • Your program is called `HelloApplication`, not `App`. You also don’t supply the full code so helping directly is not possible, no [mcve]. What package is HelloApplication in? You are doing a relative lookup in your comment so the resource needs to be in the same directory as your application class *in the build output*, is it? To add additional information, add it as code formatted in the question with an appropriate comment about the update of your edit. And link to the info you have already tried so time is not wasted providing duplicate advice. – jewelsea Nov 01 '21 at 15:42
  • i have corrected – it_would_be_better Nov 01 '21 at 15:56
  • `style.css` is in a different package to `com.example.applibrary`. It is in the unnamed package. To find it use the resource lookup string `/style.css`. Note the prefixed `/` and no `s` on the end because your question stats it is called `style.css` not `styles.css`. If you load the css in the scene you don’t also need to load it in the fxml. For some reason you did not provide the fxml. But you need to check what it is doing because your error message in the question states that the css lookup was performed twice and wrong in both cases. – jewelsea Nov 01 '21 at 16:26
  • The API you use to find your stylesheet is explained in javadoc [scene.getStyleSheets()](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/Scene.html#getStylesheets()). When using that API, the leading `/` isn't required, but I think it is still good to have it there, as it is more intuitive. I find it a easier to always work with `getResource()`, e.g. `scene.getStylesheets().add(.getResource("").toExternalForm())`, then the process for loading fxml and css is the same and you can debug the lookup path with System.out.println. – jewelsea Nov 01 '21 at 17:42
  • 1
    For showing your project directory, use the `tree` command and put the tree output as text (surrounded with html `pre` tags) in the question. This will then accurately reflect your directory structure and file locations for all of your files. If you don't have a `tree` command installed on your OS, you can lookup how to install one. – jewelsea Nov 01 '21 at 18:11

0 Answers0