Use controls from another form/controller in JavaFX 8 -
i have javafx 8 application...
mainapp.java
parent root; root = fxmlloader.load(getclass().getresource("/fxml/mainform.fxml")); scene scene = new scene(root); stage.setscene(scene); stage.show(); the mainform.fxml defines controller fx:controller="maincontroller" , controller contains text field textfieldusername.
@fxml protected textfield textfieldusername; then, there second form auxform.fxml controller fx:controller="auxcontroller". second form included in mainform.fxml this:
<content> <fx:include source="auxform.fxml" /> </content> now need value of textfieldusername. value needed in second controller have no idea how this. first idea public class auxcontroller extends maincontroller have controls available doesn't work.
add fx:id fx:include:
<content> <fx:include fx:id="auxform" source="auxform.fxml" /> </content> now can inject controller included fxml (the "nested controller") "main controller". assuming auxform.fxml has fx:controller="auxcontroller" can do:
public class maincontroller { @fxml private auxcontroller auxformcontroller ; public void initialize() { // call methods need on auxformcontroller ... // ... } // ... } the rule here append word controller value of fx:id form field name "nested controller".
now can define methods need in auxcontroller (e.g. public string getusername() {...}) , invoke them main controller when need.
for example, if needed provide data auxcontroller, provide property there set or bind maincontroller:
public class auxcontroller { private final stringproperty username ; public stringproperty usernameproperty() { return username ; } //... } then in main controller's initialize() method can do
auxformcontroller.usernameproperty().bind( textfieldusername.textproperty()); now username.get() give text in text field.
see fxml documentation more details.
Comments
Post a Comment