This is my App.java file
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("App.fxml"));
Scene scene = new Scene(root);
String css= this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Application.launch(args);
}
}
This is my SwitchController.java file
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class SceneController {
private Stage stage;
private Scene scene;
private Parent parent;
public void switchToApp(ActionEvent event) throws IOException {
parent = FXMLLoader.load(getClass().getResource("App.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
String css = this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
public void switchToApp2(ActionEvent event) throws IOException {
parent = FXMLLoader.load(getClass().getResource("App2.fxml"));
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
String css = this.getClass().getResource("application.css").toExternalForm();
scene.getStylesheets().add(css);
scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
}
This is my application.css file
.root{
-fx-background-color: "red";
}
#Sc1{
-fx-alignment: center;
}
#Sc2{
-fx-alignment: center;
-fx-text-fill: "GREEN";
-fx-outline:none;
}
.label{
-fx-font-size: 20;
-fx-font-family: "Lucida Console";
-fx-text-fill: "white";
-fx-alignment: center;
}
#titleLebel{
-fx-font-size: 60;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
this is my App2.fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneController">
<children>
<Button fx:id="Sc1" layoutX="197.0" layoutY="272.0" mnemonicParsing="false" onAction="#switchToApp" prefHeight="84.0" prefWidth="187.0" text="Switch to Scene 1" />
<Label layoutX="1.0" layoutY="50.0" prefHeight="17.0" prefWidth="600.0" text="This is the Scene 2" />
</children>
</AnchorPane>
This is my App.fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SceneController">
<children>
<Label fx:id="titleLebel" layoutX="1.0" layoutY="143.0" prefHeight="17.0" prefWidth="600.0" text="Level 1" />
<Label fx:id="secondlevewl" layoutX="2.0" layoutY="242.0" prefHeight="17.0" prefWidth="600.0" text="this is some css styling" />
<Button fx:id="Sc2" layoutX="213.0" layoutY="292.0" mnemonicParsing="false" onAction="#switchToApp2" prefHeight="78.0" prefWidth="177.0" text="Switch to Scene 2" />
</children>
</AnchorPane>
I am just a newbie to learning JavaFX and Java from [Bro code]https://www.youtube.com/watch?v=9XJicRt_FaI&ab_channel=BroCode do you guys have any suggestions please comment thank you
CodePudding user response:
As @n247s and @kleopatra suggested, you need to provide what error you are getting and have a better naming conventions so that the readers can get the logic easily.
Having said that, one thing I can easily notice is the way you assign the stylesheet. First you are assigning the stylesheet to the scene and then you are creating or initiating it, which either it causes a NullPointerExecption or the stylesheet has no impact on the scene. You need modify that line to assign it after the scene creation.
Something like in below code:
public class SceneController {
private Stage stage;
private Scene scene;
private Parent parent;
public void switchToApp(ActionEvent event) throws IOException {
parent = FXMLLoader.load(getClass().getResource("App.fxml"));
addScene(event);
}
public void switchToApp2(ActionEvent event) throws IOException {
parent = FXMLLoader.load(getClass().getResource("App2.fxml"));
addScene(event);
}
private void addScene(ActionEvent event){
stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
scene = new Scene(parent);
String css = this.getClass().getResource("application.css").toExternalForm();
// Set the stylesheet after the scene creation
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
}
}