JavaFX how to inject new FXML content to current Scene

后端 未结 1 650
情深已故
情深已故 2021-01-16 02:58

I have an app, which has HomeScene.fxml file with headers and menu. HomeScene has also dashboardPane, which should be changed dynamically after menu button is being pressed.

相关标签:
1条回答
  • 2021-01-16 03:30

    Yes, you should do this to keep scene graph low and you will benefit from better performance , what i do is create dynamic container :

    @FXML
    private ScrollPane dynamicNode;
    

    Scroll pane is a good choice.

    This is put to MainController.

    I have main controller different from others , main controller is actually the only one i initialize, so in your main program class whatever you call it :

    private static MainViewController mainViewController;
    

    ...

     private static BorderPane loadMainPane() throws IOException {
    
            FXMLLoader loader = new FXMLLoader();
            loader.setController(mainViewController);
            BorderPane mainPane = (BorderPane) loader.load(
                    CsgoRr.class
                    .getResourceAsStream(Info.Resource.FXML_FILE_MAIN));
            mainPane.getStylesheets().add(CsgoRr.class.getResource("path...style.css").toString());
    
            return mainPane;
        }
    

    Dont forget to create static accessor, other controllers that i have are usually not created this way , i use fx:controller in fxml to specify what controller should be for which fxml , its usually handy to have mainController accessable.

    So to change your views create in your main controller methods that are connected to your menu with whose you change views

    @FXML
    private void setViewPreferences() {
        setView(Info.Resource.FXML_FILE_PREFERENCES);
    }
    
    @FXML
    private void setViewProductPage() {
        setView(Info.Resource.FXML_FILE_PRODUCT_PAGE);
    }
    

    Currently in dynamicNode is helper to see what exactly is the current selected, its

    private String currentlyInDynamicPane;//not important
    

    Here is setView

       public void setView(String fxmlPath) {
             dynamicNode.setContent(getView(fxmlPath));
                currentlyInDynamicPane = fxmlPath;
       }
    
    
      public Node getView(String fxmlPath) {
            try {
                return new FXMLLoader(getClass().getResource(fxmlPath)).load();
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }
        }
    

    So when you click left menu you swap FXML files, you can make sure that you have some default FXML shown at the start or when nothing in menu is selected as well.

    This is the way i do it, roughly.

    So think about YOUR DASHBOARD as DynamicPane,

    0 讨论(0)
提交回复
热议问题