1

in my project, I need to copy the content of an specific HBox from some FXML files acording to the menu clicked, and put it in the HBox related to the content of the main page.

Bellow you can see the hierarchy of the project, and this aproach's goal is to have a global page, changing only the content of this HBox. The HBox in the main and in the other pages has the ID "conteudo".

I was able to copy and paste the content, but it can only happen once. After caling clear() or removeAll() once, it does not work anymore. There is no error also, and I checked and "conteudo" is still there after the remove/clear of it's childrens, any idea why it happens only once?

Note 1: Using ((HBox) fxmlAldeia.lookup("#conteudo")).getChildren() I get acess to the content inside "conteudo".

Note 2: getCenter does not help, because I get only a part of the center.

Note 3: I can not build the content of "conteudo" using java. I do that using Scenebuilder to speedUp, thats why I need to load it from the FXML.

Hierarchy:

Hierarchy

Código

package simulador;



public class Aldeia extends Application {

    private static Scene sceneAldeia;
    protected static BorderPane fxmlAldeia;
    protected static BorderPane fxmlEdfPrincipal;

    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("TW Fake");

        fxmlAldeia = FXMLLoader.load(getClass().getResource("Aldeia.fxml"));
        fxmlEdfPrincipal = FXMLLoader.load(getClass().getResource("EdfPrincipal.fxml"));

        sceneAldeia = new Scene(fxmlAldeia);
        primaryStage.setScene(sceneAldeia);
        primaryStage.show();
    }

    public static void changeScreen(String src) throws IOException {
        // limpa conteúdo
        ((HBox) fxmlAldeia.lookup("#conteudo")).getChildren().clear();

        switch (src) {
        case "aldeia":
            ((HBox) fxmlAldeia.lookup("#conteudo")).getChildren()
                .addAll(((HBox) fxmlAldeia.lookup("#conteudo")).getChildren());
            break;
        case "edfPrincipal":
            ((HBox) fxmlAldeia.lookup("#conteudo")).getChildren()
                .addAll(((HBox) fxmlEdfPrincipal.lookup("#conteudo")).getChildren());
            break;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Madson Paulo
  • 57
  • 1
  • 9
  • 1
    You cannot use nodes multiple times in the same or in different scenes. `((HBox) fxmlAldeia.lookup("#conteudo")).getChildren().addAll(((HBox) fxmlAldeia.lookup("#conteudo")).getChildren());` would result in an exception if there were any children left since this would require the node to be added and deleted from the same parent. In the `"edfPrincipal"` case you simply move the nodes from one parent to the other. – fabian Mar 08 '18 at 21:17
  • In addition to the comment above: why are you doing this with lookups? The supported approach here is to use the controllers for the FXML files, setting up some form of communication between them if needed (see, e.g. https://stackoverflow.com/questions/14187963). – James_D Mar 08 '18 at 21:23
  • @fabian, not sure if I understood right..Were is exactly the problem? Because using removeAll/clear I remove all the childrem from #conteudo, and then I can set children from another FXML, but works only once. What should I do then? – Madson Paulo Mar 08 '18 at 21:41
  • @James_D, because in the main FXML page, I change only the content of part of the center, so any change I make in anything else outside of #conteudo will be in all pages. The only way I found to find the container #conteudo was with lookups. How you recomend me to do? – Madson Paulo Mar 08 '18 at 21:43
  • James_D, I am using the controller of the main class to invoke these methods in the main. Something like: @FXML protected void abrirEdfPrincipal(MouseEvent mouse) throws IOException { Aldeia.changeScreen("edfPrincipal"); } – Madson Paulo Mar 08 '18 at 21:47
  • "How do you recommend me to do". Just use a controller and inject the `HBox` into the controller with `@FXML`, in the usual way. See [documentation](https://docs.oracle.com/javase/9/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html#controllers), or any FXML tutorial. – James_D Mar 08 '18 at 22:07

0 Answers0