java - javafx communication between controllers -
i'm new in javafx
, need in sharing data between 2 controllers.
i have simple window 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 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 click @ menuitem visluga_vid.setonaction
new mdi windows
opening. want open window mdi window
. mdiwindows
has parent (mdicanvas
), , mdicanvas
has parent - anchorpane
. mdiwindows
has it's own controller , 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 share anchorpane , mdicanvas
maincontroller
mdiwindowcontroller
open new mdiwindow
?
normally answer question "how can share between 2 controllers" "use service". if think it, need pass data, somehow persisted between controllers.in javafx world should model.
each .fxml file create should handled 1 controller or presenter. think .fxml views combined controllers modules of application. provides reusability. application should consist of 1 or more modules.
i recommend check out the afterburner fx. in example can see in action how different modules communicate each other.
Comments
Post a Comment