Passing Parameters between screens in JavaFX/FXML/SceneBuilder -
i'm attempting build simple proof of concept program using scenebuilder , fxml, consisting of 2 screens. first screen text field , button takes second screen, , second screen has label that, ideally, display whatever inside of text field when button hit. each screen has own fxml file , it's own controller. i've read up, down, , sideways fxmlloader, research points being ideal way done, still cant seem discern how use it. i'd implement in sort of "character creation" pre-game screen role playing game, players stat rolls/inventory choices moved initial screen either model calculating/processing, or second screens controller display.
okay, answer of sorts, bear me, found video 1 sahil huseynzade think made click me. thinking use fxmlloader import of sorts, somehow use during creation of controller in order access other controllers @ will. i've copied below code got working fxmlloader, comments pertaining how think fxmlloader working. sorry if explanations obtuse or poorly worded technical standpoint, or if i've been general novice/dunce throughout of this. did have further question though, expect it's bad form ask within answer, there way make information updated dynamically across windows? have 1 window have dice rolling button, who's results appear on window automatically?
package twoscreentest; import java.io.ioexception; import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.fxmlloader; import javafx.fxml.initializable; import javafx.scene.node; import javafx.scene.parent; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.label; import javafx.scene.control.textfield; import javafx.stage.stage; public class screen1controller implements initializable { @fxml public label label; @fxml public button button; @fxml private textfield textcharname; public string testtext; @override public void initialize(url url, resourcebundle rb) { label.settext(byte.tostring((byte) (math.floor(math.random()*6)+1))); } @fxml private void btnsend(actionevent event) throws ioexception { playerinfo player = new playerinfo(textcharname.gettext(), double.parsedouble(label.gettext())); // creating temporary container. //((node)event.getsource()).getscene().getwindow().hide(); close previous window, testing easier disable it. fxmlloader loader = new fxmlloader(); // creating fxmlloader loader.setlocation(getclass().getresource("screen2.fxml")); // telling proper fxml file. loader.load(); // loading said fxml. parent p = loader.getroot(); // setting window, think how works, //probably... stage stage = new stage(); stage.setscene(new scene(p)); screen2controller screen2controller = loader.getcontroller(); // right here //don't entirely get, know i'm using loader controller, //what's "nameofcontrollerclass variablename"? screen2controller.setcurrentinfo(player); // setting empty container in // second class' variables ones in the temporary container // created earlier. screen2controller.testlabel.settext(testtext); // additional test, setting label // on second screen variable set in controller separate button. stage.show(); // create second screen. } @fxml private void btnsend2(actionevent event) { testtext = "hello world"; } }
Comments
Post a Comment