Roman
Roman

Reputation: 3941

JavaFX Java passing argument string from controller to controller

It seems that I have some sort of basic question, but I cant figure out how to solve it, I tryed everything and atm. I am in this rage mode where nothing seems to work and the concentration fades.

I have my UserController in this UserController I have a ComboBox which works perfect, I need to pass the selected city from this ComboBox as a String argument to a method in my MuseumController.

It works for the Label but not for the method which I use in the MuseumController.

Here is the relevant code from the UserController:

public void SearchM(ActionEvent event) {

    try {
            ((Node) event.getSource()).getScene().getWindow().hide();

            Stage primaryStage = new Stage();

            FXMLLoader loader = new FXMLLoader();

            Pane root = loader.load(getClass().getResource("/application/M_in_City.fxml").openStream());

            MuseenController museenController = (MuseenController) loader.getController();
            museenController.GetCityName(txtComboboxStadt.getValue());
            //museenController.GetOnlyCityName(txtComboboxStadt.getValue());
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();

    } catch (IOException e) {

        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

In this UserController im passing the value succesfully into the Label from my MuseumController, my first Idea was, why not take the Label (because the label already displays the needed city) and get the city from there, didnt worked because it shows me somehow only the default label not the updated one with the city name.

MuseumController where I have a function which needs an ObserverList and a String argument (THE CITY), the function works perfectly, I tried it with a manual string:

@FXML
private Label MuseenInStadtLbl;

@FXML
private ListView<String> ListViewMuseen;

@Override
public void initialize(URL location, ResourceBundle resources) {    
    museenInStadtModel.FillListViewStadt(ListViewMuseen, "Düsseldorf");
}

//public String only_cityname;
/*
public String GetOnlyCityName(String city) {
    return city;
}
*/

public void GetCityName(String city) {
    //only_cityname = city;
    MuseenInStadtLbl.setText("Museen in der Stadt: " + city);
}

So I need to replace "Düssedorf" with the Value from the ComboBox or the city argument from the method GetCityName

I tryed passing only the string which always resulted in setting the string to null, its because of the initialize method, but how to do it then?

This thread sadly didnt helped: Similar Problem on StackOverFlow

I tryed to provide only relevant code, the FillListViewStadt method works perfectly. If I missed something tell me and I update.

Summarized: How do I pass the selected value from the combobox in usercontroller as a string argument into the initialize from museencontroller into the function FillListViewStadt and replace the manualy enetred "Düsseldorf" with it.

Upvotes: 0

Views: 746

Answers (1)

Moh-Aw
Moh-Aw

Reputation: 3008

The initialize() method is called on loader.load(), so way before you have the chance to pass any parameters. The correct way would be to create a different method (like you did with GetCityName()) and pass the parameters there.

Upvotes: 2

Related Questions