Java - Scene Builder - User input in TextField and ComboBox disappears after switching a scenes -
i have few scenes in java project , 1 problem don't know how solve. first scene called "zadanie". there textfields , comboboxes in first scene called "zadanie". so, can see in image, wrote numbers in textfields , choosed options in comboboxes. switched on other scene clicking on "vypočítať" (button up). , switched on first scene "zadanie", in textfields , comboboxes gone. back on first scene "zadanie". please give me example of code or how keep in first scene. thank you.
the problem when switch screens, object represents screen disposed of. variables such user input disposed of. when switch screen, it's different object of same type. means values of variables such user input freshly instantiated/initialized.
solution: create global variables (for example, variables in main, should use different classes programming practices sake) store information. when user clicks switch screens, before screen switched (unloaded), store user input in global variables. when screen switched to, reload variables fields global variables.
global.java
class global { public static string namefieldvalue; public static string agefieldvalue; }
personform.java
class personform extends borderpane { button closebutton; textfield namefield; textfield agefield; public personform() { this.namefield = new textfield(...); this.agefield = new textfield(...); this.closebutton = new button("close window"); // set layouts of buttons here. if (global.namefieldvalue != null) { this.namefield.settext(global.namefieldvalue); } if (global.passwordfieldvalue != null) { this.passwordfield.settext(global.passwordfieldvalue); } personform thisform = this; // eventhandler cna use this. this.closebutton.setonaction(new eventhandler<mouseevent>() { @override public void handle(mouseevent event) { global.namefieldvalue = thisform.namefield.gettext(); global.passwordfieldvalue = thisform.passwordfield.gettext(); // switch screens. } }); } }
Comments
Post a Comment