Search code examples
javajavafxcomboboxfxmlfxmlloader

Calling a controller class method from a non-controller class method in JavaFX, combobox population


public static void loadUser(String txtFieldName, String txtFeildPassword) throws FileNotFoundException {
    File file = new File( "C:\\Users\\obiak\\Documents\\fall 2018\\application Programming\\JavaFX Workspace"
            + "\\obp937-lab5\\src\\main\\resources\\data\\users.csv" );

    Scanner scan = new Scanner(file);
    ArrayList<User> user = new ArrayList<>();
    while( scan.hasNextLine() ) {

        String line = scan.nextLine();
        String [] token = line.split(",");
        String name = token[0];
        String pass = token[1];
        String wood = token[2];
        String core = token[3];
        String length = token[4];// remember to change file back to 8.5 and ammend constructors also
        String quality = token[5];

        User newUser = new User(name, pass, wood, core, length, quality);
        user.add(newUser);

    }
    scan.close();
    System.out.println("I am here now");


    System.out.println("Right");
    for(int  i=0; i<user.size(); i++) {
        if ( txtFieldName.equals(user.get(i).getName()) && txtFeildPassword.equals(user.get(i).getName()) ) {

        WandShopController wandShopController = new WandShopController();
        FXMLLoader loader = new FXMLLoader( );
        loader.setLocation(WandShopController.class.getResource("Scene1FXML.fxml"));
        loader.setController(loader);
        wandShopController.loaduserBox(user.get(i).getWood(), user.get(i).getCore(), user.get(i).getLength(),user.get(i).getQuality());
        System.out.println("Still working");
        }

        System.out.println(" good job");
    }

}

this is the part of the code that gets user information to populate the Combobox. it is declared and defined in a NON-controler class. the code gets the user information alright, then passes it to a method called "loadUserBox" defined in the controller class.The code works up until it needs to pass the details to the controller class method. It does not fail but it just doesnt populate the comboBox. Am i calling the controller method wrongly? please help with explanations thanks. And i am using scene builder, JavaFX, FXML

public void loaduserBox(String wood, String core, String length, String quality) {
        ObservableList<String> list = FXCollections.observableArrayList( wood, core, length, quality); 
        System.out.println("Whats wrong");
        woodBox.getItems().clear();
        woodBox.getItems().addAll(list);
    }

//this part of the code takes in information to populate the comboBox. the comboBox has already been declared in the same controller class I am referring to


Solution

  • There are three problems here.

    Firstly, your FXMLLoader (loader) does not call the load() method, so nothing is actually loaded. I'm not sure if this is missing because you are trying to copy a small portion of your original codes out.

    Secondly, you called loader.setController(loader), which effectively made the FXMLLoader the controller. Notice that you didn't use wandShopController after calling wandShopController.loaduserBox().

    So this is what you need:

    WandShopController wandShopController = new WandShopController();
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(WandShopController.class.getResource("Scene1FXML.fxml"));
    loader.setController(wandShopController); // Set the correct controller
    loader.load(); // You probably need to store the root node, depending on what you are trying to do
    wandShopController.loaduserBox(user.get(i).getWood(), user.get(i).getCore(), user.get(i).getLength(),user.get(i).getQuality());
    

    Lastly, it seems that you are loading a console application (therefore the use of Scanner), but later on it becomes a JavaFX application with GUI. I'm not too sure how you managed to do that, but that definitely looks like a problem to me.