The goal is to print a label's CURRENT text. The label constantly changes throughout the program and with my current approach I am only able to retrieve the initialized value of the label.
What I am using to retrieve the controller instance:
public class ShowWindow {
public static <T> T getController(String path){
try {
FXMLLoader fxmlLoader = new FXMLLoader(ShowWindow.class.getResource(path));
Parent root = fxmlLoader.load();
return fxmlLoader.getController();
}catch(IOException ex){
ex.printStackTrace();
}
return null;
}
}
This is the block that calls the function from the main:
private void close(Stage stage) {
String name = ShowWindow.<MainController>getController(fxmlFile).nameLabel.getText();
System.out.println(name);
stage.close();
}
Main Controller
public class MainController implements Initializable {
// Label.
// Code that changes label.
}
Anything helps, and thanks in advance.
EDIT: Another show window function that seems to work when returning the fxml file's controller, but in this case it displays the file then returns the controller.
public class ShowWindow {
public static <T> T showWindow(String path){
try {
FXMLLoader fxmlLoader = new FXMLLoader(ShowWindow.class.getResource(path));
Parent root = fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(new Scene(root));
stage.showAndWait();
return fxmlLoader.getController();
}catch(IOException ex){
ex.printStackTrace();
}
return null;
}
}
root(loaded from the FXML file) in the scene graph and displays it just for the purposes of abbreviating the code in the question. Otherwise it doesn't seem that yourgetControllermethod can ever do anything useful: it can only possibly return a controller that is connected to a UI that is never displayed. - James_D