java - Pagenavigation and value passing with afterburner fx in JavaFX? -
i'm using afterburner fx di-framework.
problem is, have "rootlayout" borderpane. on top menuebar, works , in center loaded other pane on startup. want able click on button, center-pane changes new view , want pass value new view/controller.
my main class:
@override public void start(stage primarystage) throws exception { initinjector(); //borderpane rootview appview = new rootview(); scene scene = new scene(appview.getview()); primarystage.settitle("personalplanung"); primarystage.setscene(scene); primarystage.show(); }
controller/presenter of root
public class rootpresenter implements initializable { @fxml private anchorpane center; /** * initializes controller class. */ @override public void initialize(url url, resourcebundle rb) { mainview view = new mainview(); center.getchildren().add(view.getview()); } //doesn't work because must static -> anchorpane can't static public void putcenter(fxmlview fxmlview) { center.getchildren().add(fxmlview.getview()); }
presenter of view want change , pass value (e.g. selected person)
public class mainpresenter implements initializable { @inject personservice personservice; /** * initializes controller class. */ @override public void initialize(url url, resourcebundle rb) { } /** * pseudo display of persons * should switch here persoview , pass values * gets triggerd button click */ @fxml private void handlepersonsoverview() { personservice.all().foreach(person -> { system.out.println(person.getvorname()); }); }
all views empty extend fxmlview (is convention)
import com.airhacks.afterburner.views.fxmlview; public class personview extends fxmlview{ }
if understand structure correctly, think can solve viewmodel
:
public class viewmodel { private final objectproperty<node> mainview = new simpleobjectproperty(this, "mainview", null); public objectproperty<node> mainviewproperty() { return mainview ; } public final node getmainview() { return mainview.get(); } public final void setmainview(node mainview) { this.mainview.set(mainview); } }
now inject viewmodel
wherever need it. bind center of root view model's mainview property, , update mainview property other presenters:
public class rootpresenter { @fxml private borderpane root ; // think have this? @fxml private anchorpane center ; // possibly no longer need this? @inject private viewmodel viewmodel ; public void initialize() { root.centerproperty().bind(viewmodel.mainviewproperty()); mainview view = new mainview(); viewmodel.setmainview(view); } }
now presenter needs change center of root needs do:
public class somepresenter { @inject private viewmodel viewmodel ; @fxml public void somehandlermethod() { someview someview = new someview(); viewmodel.setmainview(someview.getview()); } }
to pass values new presenter, define appropriate properties , methods in presenter , invoke them when create new view:
public class mainpresenter { private final objectproperty<person> person = new simpleobjectproperty<>(this, "person") ; public objectproperty<person> personproperty() { return person ; } public final person getperson() { return person.get(); } public final void setperson(person person) { this.person.set(person); } public void initialize() { // bind person needed ... // other stuff before } // ... }
and can do
mainview mainview = new mainview(); mainpresenter mainpresenter = (mainpresenter) mainview.getpresenter(); mainpresenter.setperson(selectedperson);
Comments
Post a Comment