I'm working on JavaFX application. From the main scene we can open many new scene. There is a toggle switch on the main scene, which add a css stylesheet(dark theme) to the parent pane or remove it. What I want is to switch the theme on all scene, regardless of the other scenes are active or not. I hope somebody can help me.
Here it is the toggle switch implementation on the main scene and a try how I tried to solve my problem:
public class MainController implements Initializable {
@FXML
private AnchorPane parentPane;
SubController subcontroller;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("view\\wordsearch-view.fxml"));
ToggleSwitch button = new ToggleSwitch();
SimpleBooleanProperty isOn = button.switchOnProperty();
isOn.addListener((observable, oldValue, newValue) -> {
if(newValue){
parentPane.getStylesheets().add(Main.class.getResource("css\\style.css").toString());
try{
fxmlLoader1.load();
subcontroller= fxmlLoader1.getController();
subcontroller.darkModeON();
}catch (Exception ex){
System.out.println("unable to load tab1");
ex.printStackTrace();
}
}else{
parentPane.getStylesheets().remove(Main.class.getResource("css\\style.css").toString());
try{
fxmlLoader1.load();
subcontroller= fxmlLoader1.getController();
subcontroller.darkModeOFF();
}catch (Exception ex){
System.out.println("unable to load tab1");
ex.printStackTrace();
}
}
});
}
}
SubController:
public class SubController implements Initializable {
@FXML
private AnchorPane parentPane;
void darkModeON(){
parentPane.getStylesheets().add(Main.class.getResource("css\\style.css").toString());
}
void darkModeOFF(){
parentPane.getStylesheets().remove(Main.class.getResource("css\\style.css").toString());
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}