java - JavaFX - setText() is not working -


i cant set text of textfield. there no error textfield still empty. rest of program working, method called , system.out.println(...) prints right text. problem is, text of textfield cant set. if write textfield.settext("0"); textfield still empty. when set text textfield under public void initialize(...) works. why doesn't work in setcurrentinfo?

@fxml private textfield textfield;  private info currentinfo;   @override public void initialize(url url, resourcebundle rb) { }     public void setcurrentinfo(info currentinfo) {     textfield.settext(currentinfo.getpw1());     system.out.println(currentinfo.getpw1());     this.currentinfo = currentinfo; } 

the part of controller setcurrentinfo called:

@fxml private void handlebtn1(actionevent event) throws exception{     info info = new info(textfielda.gettext(), textfieldb.gettext());     fxmlloader loader = new fxmlloader();     loader.setlocation(getclass().getresource("fxmlpassword.fxml"));     loader.load();     stage stage;      parent root;     if(event.getsource()==btn1){         //get reference button's stage                  stage=(stage) btn1.getscene().getwindow();         //load other fxml document         root = fxmlloader.load(getclass().getresource("fxmlpassword.fxml"));     }     else{         stage=(stage) btn1.getscene().getwindow();         root = fxmlloader.load(getclass().getresource("fxmldocument.fxml"));     }     //create new scene root , set stage     scene scene = new scene(root);     stage.setscene(scene);     stage.show();     fxmlpasswordcontroller passwordcontroller = loader.getcontroller();     passwordcontroller.setcurrentinfo(info);     } 

you retrieving controller wrong fxmlloader. in particular, do:

fxmlloader loader = new fxmlloader(); loader.setlocation(getclass().getresource("fxmlpassword.fxml")); loader.load(); 

and later

fxmlpasswordcontroller passwordcontroller = loader.getcontroller(); 

in code, create fxmlloader , point fxmlpassword.fxml file. when call loader.load(), reads fxml file , creates ui defined in it, nothing result. ui created call loader.load() never displayed.

consequently, when controller loader, , use make changes ui, never see changes because instance of textfield not displayed.

the textfield displayed created when call

root = fxmlloader.load(getclass().getresource("fxmlpassword.fxml")); 

but since use static version of fxmlloader.load(...) method here, have no opportunity controller associated it.

you need refactor code follows:

@fxml private void handlebtn1(actionevent event) throws exception{     info info = new info(textfielda.gettext(), textfieldb.gettext());     stage stage;      parent root;     if(event.getsource()==btn1){         //get reference button's stage                  stage=(stage) btn1.getscene().getwindow();         //load other fxml document         fxmlloader loader = new fxmlloader();         loader.setlocation(getclass().getresource("fxmlpassword.fxml"));         root = loader.load();         fxmlpasswordcontroller passwordcontroller = loader.getcontroller();         passwordcontroller.setcurrentinfo(info);     }     else{         stage=(stage) btn1.getscene().getwindow();         root = fxmlloader.load(getclass().getresource("fxmldocument.fxml"));     }     //create new scene root , set stage     scene scene = new scene(root);     stage.setscene(scene);     stage.show(); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -