0
votes

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;
    }
}
1
I assume here you just omitted the code that places 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 your getController method can ever do anything useful: it can only possibly return a controller that is connected to a UI that is never displayed. - James_D
@James_D No thats the actual code. Its purpose wasnt to display the stage, but to just return the controller of the fxml file's controller and not an instance of it, that way I could get the correct data. As for including the root in the function, it was giving me an error if I took it out so I just left it in. Pretty new with Javafx so since then ive looked at alot of more official ways of returning data from controller to controller. - JSextonn
But it's returning a different instance of the controller, not the one linked to the UI that's displayed. Not sure how that will possibly do anything useful. - James_D
@James_D Wow really? Now I am really confused. I have a very similar function that I've used a few times and it seems to work perfectly. The only difference is that it actually displays the fxml file. I edited the function in. - JSextonn
Well, yeah, that's kind of my point. You want the controller instance that is connected to the UI instance that is displayed. Your edit shows code that does that. In your original code you are getting a controller instance that is connected to a UI instance that is not displayed. That controller instance can't possibly affect (or represent) any UI you actually see. - James_D

1 Answers

0
votes

This is where properties would come handy. Your code is currently only retrieving the initilised value, use properties to monitor when the label text changes:

label.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            System.out.println(newValue);
        }
 });