0
votes

I have a toolbar with several buttons. I want several of this buttons to load different FXML-files. The way it is done now is by rwiting the fxml file in Java code and inserting it everytime it get's called. It would be a much better choice if I could just call the FXML-file and get the correct scene. This is an example where i load a settings view:

public void showModelSettings(){
    clearPane();
    GridPane gridPane = new GridPane();
    ColumnConstraints cc1 = new ColumnConstraints();
    cc1.setPercentWidth(50);
    ColumnConstraints cc2 = new ColumnConstraints();
    cc2.setPercentWidth(50);
    gridPane.getColumnConstraints().addAll(cc1,cc2);

    RowConstraints rc1 = new RowConstraints();
    rc1.setPercentHeight(30);
    RowConstraints rc2 = new RowConstraints();
    rc2.setPercentHeight(70);
    gridPane.getRowConstraints().addAll(rc1,rc2);

    //iwModel.setImage(new Image("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"));

    gridPane.setConstraints(cbModel, 0, 0);
    gridPane.setConstraints(btnImageChooser, 1, 0);

    gridPane.setConstraints(iwModel, 0, 1, 2, 1);


    gridPane.getChildren().addAll(cbModel, btnImageChooser,iwModel);
    mainPanel.getChildren().add(gridPane);

}

I have used tabpane at an earlier date, the handy thing with this is that it can load "content" which in a way is a actionlistener that can load fxml-files. I want this functionality for my toolbar buttons.

all help will be appreciated greatly! Thanks :)

1

1 Answers

0
votes

I think maybe I solved it, can't check yet because my project has several errors in other packages. But here is the code I believe will work:

private Parent replaceSceneContent(String fxml) throws Exception {
        Parent page = (Parent) FXMLLoader.load(InventorySystem.class.getResource("/hist/inventory/gui/fxml/"+fxml), ResourceBundle.getBundle("MessageBundle",localSettings.getLocale()), new JavaFXBuilderFactory());
        Scene scene = stage.getScene();
        if(scene == null) {
            scene = new Scene(page, 340, 280);
            scene.getStylesheets().add("hist/inventory/gui/startStyle.css");    //Endre styles?
            //stage.setResizable(false);    //Må kanskje flyttes
            stage.setScene(scene);
            Organizer.getWindowFitter().setScene(scene);
        } else {
            stage.getScene().setRoot(page);
        }
        stage.sizeToScene();
        return page;
    }

    public void setScene(String scene) throws Exception {
        replaceSceneContent(scene);
    }