javafx - Variables with multiple FXML files -
i have multi scene javafx fxml application based on angela caicedo code https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1
i have added textfield scene2.fxml , scene3.fxml files each scene has own controller class screenscontroller class loads hashmap id name, node screen defined in screensframework class (main class)
public void addscreen(string name, node screen) { screens.put(name, screen); } so each time click button on screen (scene) fire actionevent , move screen
mycontroller.setscreen(screensframework.screen2id); what if possible use value in textfield on screen2 , transfer textfield on scene3. have discovered unless both fxml files loaded far not possible. desktop application. how create variable global , has life after 1 class unloaded or 1 fxml file unloaded? @ point not want database accomplish task. have developed in visual basic 6 declare global variable used through out application.
define model class:
public class model { // define properties, example: private stringproperty text = new simplestringproperty(); public stringproperty textproperty() { return text ; } public final string gettext() { return textproperty().get(); } public final void settext(string text) { textproperty().set(text); } // other properties... } instantiate in screenscontroller class , expose it:
public class screenscontroller extends stackpane { private final model model = new model() ; public model getmodel() { return model ; } // other code before... } now can bind things together:
public class screencontroller2 implements controlledscreen, initializable { private screenscontroller mycontroller ; @fxml private textfield textfield ; // other code... @override public void setscreenparent(screenscontroller parent) { this.mycontroller = parent ; // either: mycontroller.getmodel().settext(textfield.gettext()); textfield.textproperty().addlistener((obs, oldtext, newtext) -> mycontroller.getmodel().settext(newtext)); // or, depending on exact behavior want: mycontroller.getmodel().textproperty() .bindbidirectional(textfield.textproperty()); } // ... } and
public class screencontroller3 implements controlledscreen, initializable { private screenscontroller mycontroller ; @fxml private textfield textfield ; // other code ... @override public void setscreenparent(screenscontroller parent) { this.mycontroller = parent ; // again, replace next 2 lines bidirectional // binding if behavior want textfield.settext(mycontroller.getmodel().gettext()); mycontroller.getmodel().textproperty().addlistener((obs, oldtext, newtext) -> textfield.settext(newtext)); } // ... }
Comments
Post a Comment