0

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) {

}
}
  • 1
    what do you mean by _active or not_? you can access the currently visible windows by Window.getWindows - then grab their scenes and apply whatever you want. And don't quite understand why you reload the controller on toggling (at least not why you do it more than once), you have a reference to it that you can re-use. Anyway, [mcve] required – kleopatra Feb 17 '22 at 11:14
  • You can [set the user agent stylesheet](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/Application.html#setUserAgentStylesheet(java.lang.String)). "Set the user agent stylesheet used by the whole application. This is used to provide default styling for all ui controls and other nodes.". I don't necessarily recommend that, but it is possible. – jewelsea Feb 17 '22 at 12:32
  • You should be able to use [mvc](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) to create a style model which is set by your style controls and config file preferences and have the roots of your scenes and new scenes apply the style model when the model changes, that is likely a better design than what you currently have, though it is a bit more sophisticated. – jewelsea Feb 17 '22 at 12:57
  • Possible duplicate [Set default CSS stylesheet for the whole Application](https://stackoverflow.com/questions/46559981/javafx-set-default-css-stylesheet-for-the-whole-application). Note that the suggested solution uses private API, so you will need to use module trickery [like this](https://stackoverflow.com/questions/57167872/webconsolelistener-illegalaccesserror-in-javafx-12) to access the required `com.sun.javafx.cssStyleManager.getInstance().addUserAgentStylesheet(...)` API. – jewelsea Feb 17 '22 at 13:24

0 Answers0