控制器之间的javafx通信

时间:2016-10-26 09:40:40

标签: java javafx communication fxml controllers

我是gradle init中的新人,所以我需要一些帮助才能在两个控制器之间共享数据。

我有简单的窗口,菜单简单:

JavaFX

当我点击menuItem @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); } }); } 时,新的visluga_vid.setOnAction正在打开。然后我想从mdi windows打开另一个窗口。 mdi window有父(MdiWindows),MDICanvas有父 - MDICanvasAnchorPane拥有自己的控制器和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); } }); } } MDICanvas分享AnchorPane和MainController以打开新的MDIWindowController

1 个答案:

答案 0 :(得分:0)

通常,“如何在两个控制器之间共享任何内容”这一问题的答案是“使用服务”。如果你考虑一下,你只需要传递数据,这些数据以某种方式在你的控制器之间持久存在。在javaFx世界里应该是一个模型。

您创建的每个.fxml文件只能由一个控制器或演示者处理。将您的.fxml视图与控制器结合使用,将其视为应用程序的模块。这提供了可重用性。您的申请应包含一个或多个模块。

我建议您查看the afterburner fx。在这个例子中,您可以看到不同模块之间如何相互通信。