0

I am new to javafx and am trying to connect my class to my CSS file however when I use:

scene.getStylesheets().add("Viper.css");

I get the following warning:

Dec 08, 2016 9:12:54 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
WARNING: Resource "Viper.css" not found.

But when I use:

scene.getStylesheets().add(getClass().getResource("/resources/CSS/Viper.css").toExternalForm());

I get an InvocationTargetException

Here is my entire class and I am positive that the filepath is correct. I am using NetBeans IDE.

package com.GUI;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class window extends Application{


    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("OmegaBrain");

        //Create Panes
        Pane titlePane = new Pane();


        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));

        Text sceneTitle = new Text("Welcome To OmegaBrain");
        sceneTitle.setFont(Font.font("Helvetica", FontWeight.NORMAL, 20));
        grid.add(sceneTitle, 0, 0, 4, 1);



        Scene scene = new Scene(grid, 300, 275);
        scene.getStylesheets().add("/resources/CSS/Viper.css");


        primaryStage.setScene(scene);
        primaryStage.show();


        Button play = new Button("Play");
        grid.add(play, 1, 1);

        Button leaderboard = new Button("Leaderboard");
        grid.add(leaderboard, 2, 1);

        Button faq = new Button("FAQs");
        grid.add(faq, 3, 1);

        Button exit = new Button("Exit");
        grid.add(exit, 4, 1);


        play.setOnAction((ActionEvent e) -> {
         System.out.println("The play button was clicked!");
        });



    }


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



}
MarcElrick
  • 93
  • 7
  • where's located your `Viper.css` stylesheet ? – Bo Halim Dec 08 '16 at 21:23
  • @BoHalim this is the filepath: "C:\Users\Marc\Documents\NetBeansProjects\memoryGameJavaFX\src\resources\CSS\Viper.css" – MarcElrick Dec 08 '16 at 21:25
  • Possible duplicate of [How do I load a file from resource folder?](http://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) – Itai Dec 08 '16 at 21:33
  • See possible duplicate: Assuming `resources` is marked as your main resource folder, there is no need to include `/resources/` in the path. – Itai Dec 08 '16 at 21:34
  • @sillyfly how would I know if resources is marked as such? I created the resources and CSS folders in an attempt to remedy this issue and was having it before the resource folder existed and the css document was held in src outside of any subfolders – MarcElrick Dec 08 '16 at 21:42
  • Have you fixed your problem? – SedJ601 Dec 08 '16 at 22:35

2 Answers2

0

Assuming that your application is built with this hierarchy :

Application ->src -->com/GUI/window.java -->resources/CSS/Viper.css

Then this piece of code should work :

scene.getStylesheets().add(getClass().getResource("/resources/CSS/Viper.css").toExternalForm());
// or
scene.getStylesheets().add("/resources/CSS/Viper.css");
Bo Halim
  • 1,716
  • 2
  • 17
  • 23
0

You can solve it with scene.getStylesheets().add("Package Name/Viper.css"));

pzaenger
  • 11,381
  • 3
  • 45
  • 46
lio
  • 419
  • 5
  • 9