0

I am trying to give my scene a background image, however, the program keeps saying:

"Aug 25, 2022 10:52:00 AM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged INFO: Could not find stylesheet: jar:file:///C:/Users/Muhammed%20Zain%20Muneem/IdeaProjects/CSIAFXMLL/build/libs/FXMLCSIA-1.0-SNAPSHOT.jar!/java/cssstyle/style.css"

And when I am setting it in scene builder, the background image is displayed perfectly. Its only when I run the program the image doesn't display. I have tried using different images, fxml files, and I have made the images are in the right file path, and this is evident as scene builder picks it up.

my main code:

package com.example.fxmlcsia;

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

public class main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("logInPage.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 400));
        primaryStage.show();
    }


}

My FXML code:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.*?>
<?import com.jfoenix.controls.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="800.0" styleClass="root" stylesheets="@../../../../java/cssstyle/style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.fxmlcsia.controller">
   <children>
      <AnchorPane layoutX="516.0" prefHeight="400.0" prefWidth="284.0" style="-fx-background-color: #000000;">
         <children>
            <Label alignment="CENTER" contentDisplay="CENTER" layoutX="7.0" layoutY="38.0" prefHeight="29.0" prefWidth="96.0" text="Sign In" textFill="WHITE">
               <font>
                  <Font size="24.0" />
               </font>
            </Label>
            <Hyperlink layoutX="77.0" layoutY="357.0" text="Forgot Password?" textFill="#f5f5f5" underline="true" />
            <Button fx:id="signIn" blendMode="LIGHTEN" layoutX="15.0" layoutY="315.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="256.0" style="-fx-background-color: #f05241;" text="Sign In" textFill="WHITE" />
         </children>
      </AnchorPane>
      <Label contentDisplay="CENTER" layoutX="56.0" layoutY="118.0" prefHeight="53.0" prefWidth="448.0" text="Welcome to LabManager" textFill="WHITE">
         <font>
            <Font size="30.0" />
         </font>
      </Label>
      <Label layoutX="56.0" layoutY="144.0" prefHeight="123.0" prefWidth="365.0" text="Fill in the form on the right to Sign In to your Account. Don't have an account? Sign Up by clicking the button below." textFill="WHITE" wrapText="true" />
      <Button fx:id="sUbutton" blendMode="LIGHTEN" layoutX="56.0" layoutY="252.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="256.0" style="-fx-background-color: #f05241;" text="Sign Up" textFill="WHITE" />
   </children>
</AnchorPane>

CSS FIle Code:

.root {
-fx-background-image:url("../images/background.jpg");
-fx-background-size:cover;
}

I haven't added any controller file because I haven't made one yet. Thanks.

  • 1
    I have found the answer to this question. Once you select Stylesheet, simply click the gear icon beside it and click,"Switch to absolute path." This will solve the problem entirely. – muhammed zain Aug 25 '22 at 07:43
  • I strongly doubt that solves the problem "entirely". Most likely, you've only hidden the problem. I expect your application will fail to load the stylehseet when executed on a different computer, or if you move/delete your project directory. In your FXML file, you have `@../../../../java/cssstyle/style.css`. Is that where your stylesheet is located **relative** to the FXML file's location? Are the `java/cssstyle` directories included in your JAR file? – Slaw Aug 25 '22 at 19:23
  • Yes, you are indeed very correct, but I permanently fixed the issue by placing my CSS files in the "resource package" IntelliJ built for me from the start. Now it works normally and perfectly, and the image shows by just calling the stylesheet through,"cssstyle/style.css". – muhammed zain Aug 29 '22 at 07:26

0 Answers0