menu

Questions & Answers

resource isn't found - netbeans javafx error

I was experimenting the css use with javafx. My project was very simple with 2 scenes, 2 buttons. the buttons switch between the scenes. the java file containing main class is given below:

package stageandscene;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.Label;

public class StageAndScene extends Application {

Scene scene1, scene2;
@Override
public void start(Stage primaryStage) {

    Button btn = new Button();
    Button btn2= new Button();

    btn.setText("Go to Scene 2");
    btn.setOnAction(e ->primaryStage.setScene(scene2));
    Label lebel= new Label("Hi there!! You are on scene 1");

    GridPane grid = new GridPane();
    grid.setHgap(20);
    grid.setVgap(5);
    grid.addRow(1, lebel,btn);     
    grid.setAlignment(Pos.CENTER);
    scene1 = new Scene(grid, 300, 250);
    scene1.getStylesheets().add("viper.css");

    Label lebel2= new Label("Hi there!! You are on scene 2");
    btn2.setText("Go to Scene 1");
    btn2.setOnAction(e ->primaryStage.setScene(scene1));              
    GridPane grid2 = new GridPane();
    grid2.setHgap(20);
    grid2.setVgap(5);
    grid2.addRow(1, lebel2,btn2);     
    grid2.setAlignment(Pos.CENTER);
    scene2 = new Scene(grid2, 600, 550);
    scene2.getStylesheets().add("viper.css");

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene1);
    primaryStage.show();
}
public static void main(String[] args) {
    launch(args);
}


}

"viper.css" on the same project folder that looks like this:

.root{
-fx-background-color: #ff3333;
}

this program runs well, but the css file isn't working and the background color of the both scene is unchanged. and while running the program, netbeans says "resource 'viper.css' isn't found'. Could anyone suggest me how to overcome this error?

Comments:
2023-01-25 00:20:02
are you sure that your file viper.css is in same packet as this class? Try to put them both under src directory and try again.
2023-01-25 00:20:03
Well, thanks... it was this: StageAndScene/src/viper.css now i have copied that css file at on folder up. I mean now it is: StageAndScene/viper.css and bow it works !!!😊😊 thanks again...
Answers(3) :

You have to check path of your viper.css file. If you are accessing it like that then you should have both of that Main file and CSS file in same folder.

Try something like this :

scene1.getStylesheets().add(getClass.getResource("viper.css").toExternalForm());
Comments:
2023-01-25 00:20:03
It seems like I shall have to import something for that 'getClass' ... what file should i import?
2023-01-25 00:20:03
The add method expects String, but getClass.getResource("viper.css") returns URL. Therefore toExternalForm() needs to be called.
2023-01-25 00:20:03
Yes , i forgot to add toExternalForm() i will add it now

i know it's too late but it can help other people: scene.getStylesheets().add(<class_name>.class.getResource("<css_filename>.css").toExternalForm());