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.
问题:
回答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";
}
}