Passing Parameters JavaFX FXML -


how can pass parameters secondary window in javafx? there way communicate corresponding controller?

for example: user chooses customer tableview , new window opened, showing customer's info.

stage newstage = new stage(); try  {     anchorpane page = (anchorpane) fxmlloader.load(hectorgestion.class.getresource(fxmlresource));     scene scene = new scene(page);     newstage.setscene(scene);     newstage.settitle(windowtitle);     newstage.setresizable(isresizable);     if(showrightaway)      {         newstage.show();     } } 

newstage new window. problem is, can't find way tell controller customer's info (by passing id parameter).

any ideas?

recommended approach

this answer enumerates different mechanisms passing parameters fxml controllers.

for small applications highly recommend passing parameters directly caller controller - it's simple, straightforward , requires no frameworks.

for larger, more complicated applications, worthwhile investigating if want use dependency injection or event bus mechanisms within application.

passing parameters directly caller controller

pass custom data fxml controller retrieving controller fxml loader instance , calling method on controller initialize required data values.

something following code:

public stage showcustomerdialog(customer customer) {   fxmlloader loader = new fxmlloader(     getclass().getresource(       "customerdialog.fxml"     )   );    stage stage = new stage(stagestyle.decorated);   stage.setscene(     new scene(       (pane) loader.load()     )   );    customerdialogcontroller controller =      loader.<customerdialogcontroller>getcontroller();   controller.initdata(customer);    stage.show();    return stage; }  ...  class customerdialogcontroller() {   @fxml private label customername;   void initialize() {}   void initdata(customer customer) {     customername.settext(customer.getname());   } } 

a new fxmlloader constructed shown in sample code i.e. new fxmlloader(location). location url , can generate such url fxml resource by:

new fxmlloader(getclass().getresource("sample.fxml")); 

be careful not use static load function on fxmlloader, or not able controller loader instance.

fxmlloader instances never know domain objects. not directly pass application specific domain objects fxmlloader constructor, instead you:

  1. construct fxmlloader based upon fxml markup @ specified location
  2. get controller fxmlloader instance.
  3. invoke methods on retrieved controller provide controller references domain objects.

this blog (by writer) provides alternate, similar, example.

setting controller on fxmlloader

customerdialogcontroller dialogcontroller =      new customerdialogcontroller(param1, param2);  fxmlloader loader = new fxmlloader(     getclass().getresource(         "customerdialog.fxml"     ) ); loader.setcontroller(dialogcontroller);  pane mainpane = (pane) loader.load(); 

you can construct new controller in code, passing parameters want caller controller constructor. once have constructed controller, can set on fxmlloader instance before invoke load() instance method.

to set controller on loader (in javafx 2.x) cannot define fx:controller attribute in fxml file.

due limitation on fx:controller definition in fxml, prefer getting controller fxmlloader rather setting controller fxmlloader.

having controller retrieve parameters external static method

this method exemplified sergey's answer javafx 2.0 how-to application.getparameters() in controller.java file.

use dependency injection

fxmlloader supports dependency injection systems guice, spring or java ee cdi allowing set custom controller factory on fxmlloader. provides callback can use create controller instance dependent values injected respective dependency injection system. there sample of integrating fxml spring dependency injection system (unfortunately link dead , content gone, if knows of similar example, please edit question reference it), though it's bit clunkier using new custom controller factory features made available in javafx 2.2.

a nice, clean dependency injection approach exemplified afterburner.fx framework sample air-hacks application uses it. afterburner.fx relies on jee6 javax.inject perform dependency injection.

use event bus

greg brown, original fxml specification creator , implementor, suggests considering use of event bus communication between fxml instantiated controllers , other application logic.

the eventbus simple powerful publish/subscribe api annotations allows pojos communicate each other anywhere in jvm without having refer each other.

follow-up q&a

on first method, why return stage? method can void because giving command show(); before return stage;. how plan usage returning stage

it functional solution problem. stage returned showcustomerdialog function reference can stored external class may wish something, such hide stage based on button click in main window, @ later time. alternate, object-oriented solution encapsulate functionality , stage reference inside customerdialog object or have customerdialog extend stage. full example object-oriented interface custom dialog encapsulating fxml, controller , model data beyond scope of answer, may make worthwhile blog post inclined create one.


additional information supplied stackoverflow user named @dzim

example spring boot dependency injection

the question of how "the spring boot way", there discussion javafx 2, anserwered in attached permalink. approach still valid , tested in march 2016, on spring boot v1.3.3.release: https://stackoverflow.com/a/36310391/1281217


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -