简体   繁体   English

控制器之间的javafx通信

[英]javafx communication between controllers

I'm new in JavaFX , so I need some help in sharing data between two controllers. 我是JavaFX新手,所以我需要一些帮助来在两个控制器之间共享数据。

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. 当我点击menuItem visluga_vid.setOnAction新的mdi windows正在打开。 Then I want to open another window from mdi window . 然后我想从mdi window打开另一个mdi window MdiWindows has parent ( MDICanvas ), and the MDICanvas has parent - AnchorPane . MdiWindows有父( MDICanvas ), MDICanvas有父 - AnchorPane MDIWIndows has it's own controller and fxml file. MDIWIndows拥有自己的控制器和fxml文件。

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 ? 如何将MainController AnchorPane和MDICanvas共享到MDIWindowController以打开新的MDIWindow

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. 如果你考虑一下,你只需要传递数据,这些数据以某种方式在你的控制器之间持久存在。在javaFx世界里应该是一个模型。

Each .fxml file that you create should be handled only by one controller or presenter. 您创建的每个.fxml文件只能由一个控制器或演示者处理。 Think about your .fxml views combined with your controllers as modules of your application. 将您的.fxml视图与控制器结合使用,将其视为应用程序的模块。 This provides reusability. 这提供了可重用性。 Your application should consist of one or more modules. 您的应用程序应包含一个或多个模块。

I would recommend that you also check out the afterburner fx . 我建议您也查看加力燃烧室fx In this example you can see in action how different modules communicate with each other. 在此示例中,您可以看到不同模块之间如何相互通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM