1

I'm new in JavaFX, so I need some help in sharing data between two controllers.

I have simple window which has simple menu:

@FXML
Label labelLabel;

@FXML
MenuItem sbor;

@FXML
MenuItem alim_poluch;

@FXML
MenuItem paragraphs;

@FXML
MenuItem poluch_cat;

@FXML
MenuItem visluga_vid;


@FXML
AnchorPane menuPane;


@FXML
MDICanvas mdiCanvas;

@FXML
Tab tabOne;

@FXML
VislugaVidController vid;

@FXML
Tab tabTwo;

@FXML
public void initialize() {

    MDICanvas mdiCanvas = new MDICanvas(MDICanvas.Theme.DEFAULT);
    menuPane.getChildren().add(mdiCanvas);

    AnchorPane.setBottomAnchor(mdiCanvas, -1d);
    AnchorPane.setLeftAnchor(mdiCanvas, 0d);
    AnchorPane.setTopAnchor(mdiCanvas, 0d);//Button place
    AnchorPane.setRightAnchor(mdiCanvas, 0d);


    sbor.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            Stage stage = new Stage();
            AnchorPane pane = null;
            try {
                pane = FXMLLoader.load(getClass().getResource("/fxml/spr_pocht_sbor.fxml"));
            } catch (IOException e) {
                System.err.print("Can't open the resource file");
                e.printStackTrace();
            }
            stage.setTitle("Почтовый сбор для перевода алиментов");
            stage.setResizable(false);
            Scene scene = new Scene(pane, 600, 450);
            stage.setScene(scene);
            stage.show();
        }
    });

    alim_poluch.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            Stage stage = new Stage();
            AnchorPane pane = null;
            try {
                pane = FXMLLoader.load(getClass().getResource("/fxml/spr_alim_poluch.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            stage.setResizable(false);
            stage.setTitle("Справочник получателей алиментов");
            Scene scene = new Scene(pane, 800, 640);
            stage.setScene(scene);
            stage.show();
        }
    });

    paragraphs.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            Stage stage = new Stage();
            AnchorPane pane = null;
            try {
                pane = FXMLLoader.load(getClass().getResource("/fxml/paragraf.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            stage.setResizable(false);
            stage.setTitle("Параграф назначения денежных средств");
            Scene scene = new Scene(pane, 300, 450);
            stage.setScene(scene);
            stage.show();
        }
    });

    poluch_cat.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            Stage stage = new Stage();
            AnchorPane pane = null;
            try {
                pane = FXMLLoader.load(getClass().getResource("/fxml/poluch_cat.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }

            stage.setResizable(false);
            stage.setTitle("Категории получателей");
            Scene scene = new Scene(pane, 600, 450);
            stage.setScene(scene);
            stage.show();
        }
    });

    visluga_vid.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {


            AnchorPane content = null;
            try {
                content = FXMLLoader.load(getClass().getResource("/fxml/visluga_vid.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            MDIWindow mdiWindow = new MDIWindow("1", new ImageView("/assets/WindowIcon.png"), "Виды выслуг", content);
            Button buttonMaximize = mdiWindow.getBtnMaximize();
            buttonMaximize.setDisable(true);
            mdiWindow.setMaxSize(350, 450);
            mdiWindow.setBtnMinimize(buttonMaximize);
            mdiCanvas.addMDIWindow(mdiWindow);


        }
    });


}

When I click at menuItem visluga_vid.setOnAction new mdi windows is opening. Then I want to open another window from mdi window. MdiWindows has parent (MDICanvas), and the MDICanvas has parent - AnchorPane. MDIWIndows has it's own controller and fxml file.

public class VislugaVidController {

    @FXML
    TableView vislugaVidTable;

    @FXML
    TextField naim_vislugaField;

    @FXML AnchorPane menuPane;

    @FXML
    public void initialize() {

        //main.init(this);



        vislugaVidTable.setOnMousePressed(event ->  {

            if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
                    System.out.println(vislugaVidTable.getSelectionModel().getSelectedItem());
                    AnchorPane content = null;
                    try {
                        content = FXMLLoader.load(getClass().getResource("/fxml/visluga_nadb.fxml"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    MDIWindow mdiWindow = new MDIWindow("1", new ImageView("/assets/WindowIcon.png"), "Надбавки", content);
                    Button buttonMaximize = mdiWindow.getBtnMaximize();
                    buttonMaximize.setDisable(true);
                    mdiWindow.setMaxSize(350, 450);
                    mdiWindow.setBtnMinimize(buttonMaximize);
                    mdiCanvas.addMDIWindow(mdiWindow);



                }

        });

    }


}

How can I share AnchorPane and MDICanvas from MainController to MDIWindowController to open new MDIWindow?

Draken
  • 3,134
  • 13
  • 34
  • 54
Artificial
  • 23
  • 1
  • 6
  • 1
    Create a model to save your data and then share the model across different controllers. Its the important part that is lacking in you logic of MVC. – Blip Oct 26 '16 at 10:11
  • You can add `getter` method to the controllers and call them to instances of the controllers..Although you should learn `Model View Controller Pattern'(MVC) that will help you build great applications. – GOXR3PLUS Oct 26 '16 at 10:14
  • For a simple example of using MVC in JavaFX to share data between controllers, as @Blip suggests see the answer to http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx. The [afterburner.fx](http://afterburner.adam-bien.com/) framework linked in Eirini's answer provides a simplified mechanism for passing the same model instance to controllers. – James_D Oct 26 '16 at 12:00
  • Big thanks for answers, but, actually, I no need to share model, like User or Person or another. I need to share an interface-object or fxml-object, like AnchorPane or Button or Label, etc. – Artificial Oct 27 '16 at 07:35

1 Answers1

0

Normally the answer to the question "how can I share anything between two controllers" is "use a service". If you think about it, you only need to pass data, that are somehow persisted between your controllers.In the javaFx world that should be a model.

Each .fxml file that you create should be handled only by one controller or presenter. Think about your .fxml views combined with your controllers as modules of your application. This provides reusability. Your application should consist of one or more modules.

I would recommend that you also check out the afterburner fx. In this example you can see in action how different modules communicate with each other.

Eirini Graonidou
  • 1,506
  • 16
  • 24
  • could you explain how the link that you have posted going to help in solving the problem. – Blip Oct 26 '16 at 11:09
  • I posted the link as a recommendation for understanding the MVC in javaFX terms. Ok the afterburner goes further and uses DI but I find the example really useful. At least it helped me to understand. From my perspective, the problem starts when one passes a controller to another controller. What would happen if the application grew? – Eirini Graonidou Oct 26 '16 at 11:36
  • @Blip That is a common evolution in writing these types of applications: first you find you need to communicate between controllers, then (after some time) you discover the best way to do this is by sharing a model instance whose data you observe and update. Then you find that the mechanics of actually sharing that model instance get cumbersome, and a DI framework eases the pain considerably. – James_D Oct 26 '16 at 12:40