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?
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.