0

I'm attempting to build a simple proof of concept program using SceneBuilder and FXML, consisting of two screens. The first screen is just a text field and a button that takes you to the second screen, and the second screen has just a label that, ideally, will display whatever was inside of the text field when the button was hit. Each screen has its own FXML file and it's own controller. I've read up, down, and sideways about FXMLLoader, as my research points to that being the ideal way to get this done, but I still cant seem to discern how to properly use it. Ultimately I'd like to implement this in a sort of "character creation" pre-game screen for a role playing game, where the players stat rolls/inventory choices are moved from the initial screen to either a model for calculating/processing, or to the second screens controller for display.

Fgsfds
  • 11
  • 1
  • 2
  • 6
  • Are the 2 screens initially opened ? or the the second screen opens on the button click ? – Mohamed Benmahdjoub May 30 '15 at 20:50
  • The second screen is set to open on the button click. – Fgsfds May 30 '15 at 21:11
  • See http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml/14190310#14190310 – James_D May 30 '15 at 22:20
  • Thanks @James_D, the thing is though, I've read that, and I'm still not entirely getting it. I haven't found a good breakdown of the syntax of an FXMLLoader anywhere, nor how I can use it to gain access to one controller's variables from another, or to access a controllers variables from a non-controller class. I know I create a new FXMLLoader that uses getClass and getResources to get the other FXML file, and that on the line below that I should get the controller somehow, but that's about where I loose the trail. – Fgsfds May 31 '15 at 01:09
  • The syntax is just the same as for any other Java object; it is just Java. You probably need to post some code to show what you are doing. – James_D May 31 '15 at 02:12
  • Should I do so in a new question, or can I quote code in comments? (Is there even sufficient space in a comment for that?) Sorry if I'm a man of a million questions and only sporadic knowledge, I'm trying to self-teach myself via freely accessible means, and this is one part I just cant get my head around without someones help, so I thank you deeply. Based on what I'm understanding from corpico's answer below is that I need to create new FXMLLoaders, as they're objects, and then later, in the class that I created that object, I can use "nameofloader.fxidofUIelement.dosomething()"? – Fgsfds May 31 '15 at 16:44

1 Answers1

-1

Okay, this is an answer of sorts, bear with me, I found a video by a one Sahil Huseynzade that I think finally made this click for me. I was thinking that I could use FXMLLoader as almost an import of sorts, that I could somehow use during the creation of my controller in order to access other controllers at will. I've copied below some code I got working with an FXMLLoader, with some comments pertaining to how I think FXMLLoader is working. Sorry if the explanations are obtuse or poorly worded from a technical standpoint, or if I've been a general novice/dunce throughout all of this. I did have a further question though, I expect that it's bad form to ask it within an answer, is there a way to make it so that information is updated dynamically across windows? Like could I have one window have a dice rolling button, who's results appear on another 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 the temporary container.
        //((Node)event.getSource()).getScene().getWindow().hide(); This is to close the previous window, for testing it was easier to disable it.
        FXMLLoader loader = new FXMLLoader(); // Creating a the FXMLLoader
        loader.setLocation(getClass().getResource("Screen2.fxml")); // Telling it to get the proper FXML file.
        loader.load(); // Loading said FXML.
        Parent p = loader.getRoot(); // Setting up the window, I think I get how this works,
        //probably...
        Stage stage = new Stage();
        stage.setScene(new Scene(p));
        Screen2Controller screen2controller = loader.getController(); // This right here I 
        //don't entirely get, I know that I'm using the loader to get the controller, but
        //what's with the "NameofControllerClass variablename"?
        screen2controller.setCurrentInfo(player); // Setting the empty container in
        // the second class' variables to the ones in the the temporary container that
        // I created earlier.
        screen2controller.testlabel.setText(testtext); // An additional test, setting a label
        // on the second screen to a variable I set in this controller with a separate button.
        stage.show(); // Create the second screen.

    }

    @FXML
    private void btnSend2(ActionEvent event) {

        testtext = "Hello world";
    }

}
Fgsfds
  • 11
  • 1
  • 2
  • 6